Re: Foreign key dependent model's data not getting saved !
On Sat, Jun 25, 2011 at 1:42 PM, CareerDhaba techwrote: > Hey, > > Thanks Karen for the reply. I have been trying many things on this form and > trying to save both the models data using one form in a single instance. I > did try the commit=False method too but it gave an error which i couldnt > figure out. Everytime i try doing something new an unknown error/exception > pops up that too in some random file and location :(. > > No, really, code does not behave this way. Errors do not pop up randomly in random locations. You need to learn how to interpret the debug information the system is providing you. > This is the traceback which i got now after changing code > > Environment: > > > Request Method: POST > Request URL: http://127.0.0.1:8000/cd/registration/register/ > > Django Version: 1.3 > [snip] > Traceback: > File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in > get_response > 111. response = callback(request, *callback_args, > **callback_kwargs) > File "C:\Users\pradeep\Desktop\cd\registration\views.py" in register > 23. return direct_to_template('success.html',{}) > File "C:\Python27\lib\site-packages\django\views\generic\simple.py" in > direct_to_template > 26. c = RequestContext(request, dictionary) > File "C:\Python27\lib\site-packages\django\template\context.py" in __init__ > 177. self.update(processor(request)) > File "C:\Python27\lib\site-packages\django\core\context_processors.py" in > debug > 53. if settings.DEBUG and request.META.get('REMOTE_ADDR') in > settings.INTERNAL_IPS: > > Exception Type: AttributeError at /cd/registration/register/ > Exception Value: 'str' object has no attribute 'META' > > While the ultimate error here appears to be in Django code, line 23 of your views.py file is higher up in the traceback, and that is the line causing the problem. You are not passing the correct parameters to direct_to_template. I'm not going to lay out what the correct parameters should be (you could work that out for yourself by reading the docs), because you shouldn't really be using direct_to_template there. The common pattern for processing forms in a view is laid out here: https://docs.djangoproject.com/en/1.3/topics/forms/#using-a-form-in-a-view (Looking at that example I notice it should be updated to use render, not render_to_response, but the overall pattern for the view is still correct. Note that in the case of successful POST processing, the code in that view returns a redirect, not a regular response. This is a general web application pattern, not specific to Django, you can read about the reasons for it here for example: http://en.wikipedia.org/wiki/Post/Redirect/Get ) Besides not redirecting after a successful completion, you are using two different templates for different flows through your view, which is also probably not what you want. You are using register.html for the initial get of the page, and regcheck1.html for the case where the posted forms don't validate successfully. In general you should be using the same template for these cases. Karen -- http://tracey.org/kmt/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Foreign key dependent model's data not getting saved !
Hey, Thanks Karen for the reply. I have been trying many things on this form and trying to save both the models data using one form in a single instance. I did try the commit=False method too but it gave an error which i couldnt figure out. Everytime i try doing something new an unknown error/exception pops up that too in some random file and location :(. This is the traceback which i got now after changing code Environment: Request Method: POST Request URL: http://127.0.0.1:8000/cd/registration/register/ Django Version: 1.3 Python Version: 2.7.1 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'registration'] Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.csrf.CsrfResponseMiddleware') Traceback: File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "C:\Users\pradeep\Desktop\cd\registration\views.py" in register 23. return direct_to_template('success.html',{}) File "C:\Python27\lib\site-packages\django\views\generic\simple.py" in direct_to_template 26. c = RequestContext(request, dictionary) File "C:\Python27\lib\site-packages\django\template\context.py" in __init__ 177. self.update(processor(request)) File "C:\Python27\lib\site-packages\django\core\context_processors.py" in debug 53. if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: Exception Type: AttributeError at /cd/registration/register/ Exception Value: 'str' object has no attribute 'META' On Sat, Jun 25, 2011 at 11:04 PM, Karen Traceywrote: > On Sat, Jun 25, 2011 at 1:00 PM, CareerDhaba tech wrote: > >> The user model is getting saved with username and password but the >> userprofile isn;t :( > > > That is a somewhat misleading description of the problem since the code as > you have posted it would not just fail to save the userprofile it would > raise an exception on the attempt to pass user to RegForm, since RegForm as > you have shown it is not expecting user as a keyword argument. > > The right way to handle saving model forms which have explicitly excluded > necessary data is described here: > https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form > > In this case you would want to save the originally-created RegForm instance > uprofile with comit=False, then set the user attribute to the user returned > by the other form save, then save the user profile instance: > > up = uprofile.save(commit=False) > up.user = userid > up.save() > > Karen > -- > http://tracey.org/kmt/ > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Foreign key dependent model's data not getting saved !
On Sat, Jun 25, 2011 at 1:00 PM, CareerDhaba techwrote: > The user model is getting saved with username and password but the > userprofile isn;t :( That is a somewhat misleading description of the problem since the code as you have posted it would not just fail to save the userprofile it would raise an exception on the attempt to pass user to RegForm, since RegForm as you have shown it is not expecting user as a keyword argument. The right way to handle saving model forms which have explicitly excluded necessary data is described here: https://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form In this case you would want to save the originally-created RegForm instance uprofile with comit=False, then set the user attribute to the user returned by the other form save, then save the user profile instance: up = uprofile.save(commit=False) up.user = userid up.save() Karen -- http://tracey.org/kmt/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Foreign key dependent model's data not getting saved !
Whats the problem with this code ? *models.py *class userprofile(models.Model): user = models.OneToOneField(User) Firstname = models.CharField(max_length=20) Middlename = models.CharField(max_length=20) Surname = models.CharField(max_length=20) Gender = models.CharField(max_length=1, blank=False, choices=GENDER_CHOICES) Date_of_Birth = models.DateField() Hometown = models.ForeignKey(city) StudentType = models.CharField(max_length=20, blank=False, choices=STUDENT_CHOICES) * forms.py *from django.contrib.auth class RegForm(ModelForm): Date_of_Birth = forms.DateField(widget=SelectDateWidget(years=[y for y in range(1960,2005)])) class Meta: model = userprofile exclude = ('user',) * views.py *from django.contrib.auth.forms import UserCreationForm def register(request): if request.method == 'POST': form = UserCreationForm(request.POST) uprofile = RegForm(request.POST) if form.is_valid() and uprofile.is_valid(): userid = form.save() a = RegForm(request.POST,user = userid) a.save() return direct_to_template('success.html',{}) else: userform = UserCreationForm() RF = RegForm() return render_to_response('register.html',{ 'uf': userform, 'rf': RF, }) return render_to_response('regcheck1.html',{}) * regcheck1.html *{% csrf_token %} {{ rf.as_p }} {{ uf.as_p }} * *The user model is getting saved with username and password but the userprofile isn;t :( -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.