Guest User

Untitled

a guest
Mar 15th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. #settings.py
  2. AUTH_PROFILE_MODULE = 'client.Client'
  3. LOGIN_REDIRECT_URL = '/acces-client/'
  4. LOGIN_URL = '/acces-client/login/'
  5. LOGOUT_URL = '/acces-client/logout/'
  6.  
  7. #models.py
  8. #-*- encoding: utf-8 -*-
  9.  
  10. #Import Django
  11. from django.db import models
  12. from django.contrib.auth.models import User
  13. from django.contrib.localflavor.ch import ch_states
  14. from django.utils.translation import ugettext as _
  15.  
  16.  
  17. class Client(models.Model):
  18.     """
  19.         Model pour la base de donnée des clients
  20.     """
  21.     user = models.ForeignKey(User)
  22.     company = models.CharField(max_length=512, verbose_name=_(u"Société"), blank=True)
  23.     adress = models.CharField(max_length=512, verbose_name=_(u"Adresse *"))
  24.     zipcode = models.IntegerField(max_length=4, verbose_name=_(u"Code postal *"))
  25.     city = models.CharField(max_length=128, verbose_name=_(u"Ville *"))
  26.     website = models.URLField(verbose_name=_(u"Site internet"), blank=True)
  27.     phone = models.CharField(max_length=128, verbose_name=_(u"Téléphone *"))
  28.     mobile = models.CharField(max_length=128, verbose_name=_(u"Natel"), blank=True)
  29.     fax = models.CharField(max_length=128, verbose_name=_(u"fax"), blank=True)
  30.    
  31.     def __unicode__(self):
  32.         return self.user
  33.        
  34.  
  35. #forms.py
  36. class informationsForm(forms.Form):
  37.     """
  38.         Formulaire d'information sur l'utilisateur
  39.     """
  40.     email = forms.EmailField(label=_(u"Email*"))
  41.     password = forms.CharField(widget=forms.PasswordInput, label=_("Mot de passe*"),)
  42.     password2 = forms.CharField(widget=forms.PasswordInput, label=_("Confirmation du mot de passe*"))
  43.     lastname = forms.CharField(max_length=100, label=_(u"Nom*"))
  44.     firstname = forms.CharField(max_length=100, label=_(u"Prénom*"))
  45.     zipcode = CHZipCodeField(label=_(u"Code postal*"))
  46.     city = forms.CharField(widget=CHStateSelect)
  47.     phone = CHPhoneNumberField(label=_(u"Téléphone*"))
  48.     mobile = CHPhoneNumberField(label=_(u"Natel"), required=False)
  49.     fax = CHPhoneNumberField(label=_(u"Fax"), required=False)
  50.  
  51. #views.py
  52. @login_required
  53. def informations(request, template_name):
  54.     if request.method == 'POST':
  55.         form = informationsForm(request.POST)
  56.         if form.is_valid():
  57.             user = request.user
  58.             # we take the field names of the user to use them to extract information from the form
  59.             user_fields = fields_for_model(user).keys()
  60.             # we create a dictionary with the data that belongs to the user
  61.             new_data = dict((field, form.cleaned_data.pop(field)) for field in user_fields)
  62.             form.cleaned_data['email']
  63.             form.cleaned_data['password']
  64.             form.cleaned_data['lastname']
  65.             form.cleaned_data['firstname']
  66.             form.cleaned_data['zipcode']
  67.             form.cleaned_data['city']
  68.             form.cleaned_data['phone']
  69.             form.cleaned_data['mobile']
  70.             form.cleaned_data['fax']
  71.             # we set the new data on the user
  72.             for field in new_data:
  73.                 setattr(user, field, new_data[field])
  74.                 user.save()
  75.                 return redirect('client_informations')
  76.     else:
  77.         user = request.user
  78.         infosUser = model_to_dict(user)
  79.         try:
  80.             client = user.client_set.get()
  81.         except Client.DoesNotExist: #he return an except error
  82.             client = None
  83.             print "coucou"
  84.         infosClient = model_to_dict(client)
  85.         infosUser.update(infosClient) #I want to get the data of profil
  86.         infosForm = informationsForm(initial=infosUser)
  87.         return render_to_response(template_name, { "forms": infosForm }, context_instance = RequestContext(request))
  88.  
  89.  
  90. #error:
  91. in the information -> he go to except and i've this error:
  92. 'NoneType' object has no attribute '_meta'
  93.  
  94. I want the form view the username, email, password from django.contrib.auth user and the informations of my models
Add Comment
Please, Sign In to add comment