Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #settings.py
- AUTH_PROFILE_MODULE = 'client.Client'
- LOGIN_REDIRECT_URL = '/acces-client/'
- LOGIN_URL = '/acces-client/login/'
- LOGOUT_URL = '/acces-client/logout/'
- #models.py
- #-*- encoding: utf-8 -*-
- #Import Django
- from django.db import models
- from django.contrib.auth.models import User
- from django.contrib.localflavor.ch import ch_states
- from django.utils.translation import ugettext as _
- class Client(models.Model):
- """
- Model pour la base de donnée des clients
- """
- user = models.ForeignKey(User)
- company = models.CharField(max_length=512, verbose_name=_(u"Société"), blank=True)
- adress = models.CharField(max_length=512, verbose_name=_(u"Adresse *"))
- zipcode = models.IntegerField(max_length=4, verbose_name=_(u"Code postal *"))
- city = models.CharField(max_length=128, verbose_name=_(u"Ville *"))
- website = models.URLField(verbose_name=_(u"Site internet"), blank=True)
- phone = models.CharField(max_length=128, verbose_name=_(u"Téléphone *"))
- mobile = models.CharField(max_length=128, verbose_name=_(u"Natel"), blank=True)
- fax = models.CharField(max_length=128, verbose_name=_(u"fax"), blank=True)
- def __unicode__(self):
- return self.user
- #forms.py
- class informationsForm(forms.Form):
- """
- Formulaire d'information sur l'utilisateur
- """
- email = forms.EmailField(label=_(u"Email*"))
- password = forms.CharField(widget=forms.PasswordInput, label=_("Mot de passe*"),)
- password2 = forms.CharField(widget=forms.PasswordInput, label=_("Confirmation du mot de passe*"))
- lastname = forms.CharField(max_length=100, label=_(u"Nom*"))
- firstname = forms.CharField(max_length=100, label=_(u"Prénom*"))
- zipcode = CHZipCodeField(label=_(u"Code postal*"))
- city = forms.CharField(widget=CHStateSelect)
- phone = CHPhoneNumberField(label=_(u"Téléphone*"))
- mobile = CHPhoneNumberField(label=_(u"Natel"), required=False)
- fax = CHPhoneNumberField(label=_(u"Fax"), required=False)
- #views.py
- @login_required
- def informations(request, template_name):
- if request.method == 'POST':
- form = informationsForm(request.POST)
- if form.is_valid():
- user = request.user
- # we take the field names of the user to use them to extract information from the form
- user_fields = fields_for_model(user).keys()
- # we create a dictionary with the data that belongs to the user
- new_data = dict((field, form.cleaned_data.pop(field)) for field in user_fields)
- form.cleaned_data['email']
- form.cleaned_data['password']
- form.cleaned_data['lastname']
- form.cleaned_data['firstname']
- form.cleaned_data['zipcode']
- form.cleaned_data['city']
- form.cleaned_data['phone']
- form.cleaned_data['mobile']
- form.cleaned_data['fax']
- # we set the new data on the user
- for field in new_data:
- setattr(user, field, new_data[field])
- user.save()
- return redirect('client_informations')
- else:
- user = request.user
- infosUser = model_to_dict(user)
- try:
- client = user.client_set.get()
- except Client.DoesNotExist: #he return an except error
- client = None
- print "coucou"
- infosClient = model_to_dict(client)
- infosUser.update(infosClient) #I want to get the data of profil
- infosForm = informationsForm(initial=infosUser)
- return render_to_response(template_name, { "forms": infosForm }, context_instance = RequestContext(request))
- #error:
- in the information -> he go to except and i've this error:
- 'NoneType' object has no attribute '_meta'
- 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