reg:not able to open registration form web page

18 views
Skip to first unread message

Amitesh Sahay

unread,
Nov 15, 2017, 11:33:36 AM11/15/17
to django...@googlegroups.com
Hello Members, 

I am new to Django, and trying to create a mock Django registration page. I am using default Django "User" model to do that, and I am not customizing anything. Its a very simple form with 3 fields as follows:

'username','password','email'. Below are my python and html code details:

form.py
-------
from .models import User
from django import forms
from django.forms import ModelForm

class SignUpForm(forms.ModelForm):
   class Meta:
        model = User
        fields = ('username','password','email')
models.py
---------
from django.db import models
#from django.core.urlresolvers import reverse
from django.contrib.auth.models import User

class Registration(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
urls.py
--------
urlpatterns = [
  url(r'^register/$', views.SignUpFormView, name= 'register'),
]


views.py
--------

def SignUpFormView(request):
    user_form = 'SignUpForm'
    template_name = 'test.html'

    if request.method == 'POST':
        form = user_form(request.POST)
        if form.is_valid():
            form.save()
            #username = form.cleaned_data.get('username')
            #password = form.cleaned_data.get('password')
            #email = form.cleaned_data.get('email')
            #user.save()
            return render(request, template_name, {'form':form})


    else:
        SignUpForm()

    return render(request, 'user_info/about.html')
test.html
---------
{% extends 'user_info/base.html' %}


{% block body %}
{% block content %}


{% for error in form.errors %}
    {{ form.errors | default_errors }}
{% endfor %}

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    {% for field in form %}
        <p>

      username:<br>
      <input type="text" name="username"><br>
      password:<br>
      <input type="text" name="password"><br>
      email:<br>
      <input type="text" name="email"><br>
      {% for error in field.errors %}
           <p style="color: red">{{ error }}</p>
    {% endfor %}
        </p>
    {% endfor %}
    <button type="submit" value="Submit">sign up </button>
</form>


{% endblock %}
{% endblock %}

My issue is, when trying to launch the "register" page, its not going inside "if" condition in views.py, rather its going directly to "else" condition. 

I am tried many things under my reach, but couldn't resolve the issue, and stuck for 2 weeks now. 
Any help would be appreciated.

Regards,
Amitesh Sahay


Amitesh Sahay

unread,
Nov 17, 2017, 12:50:40 AM11/17/17
to django...@googlegroups.com
Can somebody kindly reply to my query please... I am completely stuck no where to find any help

James Schneider

unread,
Nov 17, 2017, 1:34:41 AM11/17/17
to django...@googlegroups.com


On Nov 15, 2017 8:32 AM, "'Amitesh Sahay' via Django users" <django...@googlegroups.com> wrote:
Hello Members, 

I am new to Django, and trying to create a mock Django registration page. I am using default Django "User" model to do that, and I am not customizing anything. Its a very simple form with 3 fields as follows:

'username','password','email'. Below are my python and html code details:

Snip...

views.py
--------

def SignUpFormView(request):
    user_form = 'SignUpForm'
    template_name = 'test.html'

    if request.method == 'POST':
        form = user_form(request.POST)
        if form.is_valid():
            form.save()
            #username = form.cleaned_data.get('username')
            #password = form.cleaned_data.get('password')
            #email = form.cleaned_data.get('email')
            #user.save()
            return render(request, template_name, {'form':form})


    else:
        SignUpForm()

    return render(request, 'user_info/about.html')

Snip...

My issue is, when trying to launch the "register" page, its not going inside "if" condition in views.py, rather its going directly to "else" condition. 

I am tried many things under my reach, but couldn't resolve the issue, and stuck for 2 weeks now. 
Any help would be appreciated.

There are several issues with the view composition. Please refer to the docs here and verify your syntax matches the example:


Your template code also suggests that you are seeing duplicate form fields (or perhaps they are being rendered in the source but aren't visible). It's also possible that the Django form is not being created correctly in the first place, which is why you need to manually render the form fields, when the {{ form.as_p }} should be doing that for you.

If you are submitting the form and getting the behavior you describe, I'm guessing that the if statement isn't being skipped, but the values being submitted do not match the form object in Django, causing it to fail validation, meaning that the second if statement is coming back False.

-James

Amitesh Sahay

unread,
Nov 17, 2017, 6:34:40 AM11/17/17
to django...@googlegroups.com, James Schneider
Hello James, 

Thanks a lot for reply. There are couple of things that I would like to bring to the notice

1) I did go through that part of django document before posting the issue here.
2) In that doc, in the 2nd "if" condition, its talking about cleaned_data. In one of the public forum, I was asked to remove that part from my views.py, as I am not customizing my models.py. If I use cleaned_data. I entered the below in my earlier view.py

            #username = form.cleaned_data.get('username')
            #password = form.cleaned_data.get('password')
            #email = form.cleaned_data.get('email')
            #user.save()

Please let me know if that was right? Also, can you point me to the exact changes that you want me to make in my views.py. I am sorry that I am asking this, but over the last 2 week or so, I am so frustrated that I am not able to think much on this part. 


Hello,

Regards,
Amitesh Sahay

primary :: 91-907 529 6235



On Friday 17 November 2017, 12:04:25 PM IST, James Schneider <jrschn...@gmail.com> wrote:




On Nov 15, 2017 8:32 AM, "'Amitesh Sahay' via Django users" <django...@googlegroups.com > wrote:
Hello Members, 

I am new to Django, and trying to create a mock Django registration page. I am using default Django "User" model to do that, and I am not customizing anything. Its a very simple form with 3 fields as follows:

'username','password','email'. Below are my python and html code details:

Snip...

views.py
--------

def SignUpFormView(request):
    user_form = 'SignUpForm'
    template_name = 'test.html'

    if request.method == 'POST':
        form = user_form(request.POST)
        if form.is_valid():
            form.save()
            #username = form.cleaned_data.get('usernam e')
            #password = form.cleaned_data.get('passwor d')
            #email = form.cleaned_data.get('email')
            #user.save()
            return render(request, template_name, {'form':form})


    else:
        SignUpForm()

    return render(request, 'user_info/about.html')

Snip...

My issue is, when trying to launch the "register" page, its not going inside "if" condition in views.py, rather its going directly to "else" condition. 

I am tried many things under my reach, but couldn't resolve the issue, and stuck for 2 weeks now. 
Any help would be appreciated.

There are several issues with the view composition. Please refer to the docs here and verify your syntax matches the example:


Your template code also suggests that you are seeing duplicate form fields (or perhaps they are being rendered in the source but aren't visible). It's also possible that the Django form is not being created correctly in the first place, which is why you need to manually render the form fields, when the {{ form.as_p }} should be doing that for you.

If you are submitting the form and getting the behavior you describe, I'm guessing that the if statement isn't being skipped, but the values being submitted do not match the form object in Django, causing it to fail validation, meaning that the second if statement is coming back False.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWNrWLhEzhM6649BsP5JByUXbsS7gvBVaKq2BkejKH7Rw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout
.

Mike Dewhirst

unread,
Nov 17, 2017, 5:57:12 PM11/17/17
to django...@googlegroups.com
Amitesh

I found this helpful ...

https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html

Cheers

Mike


On 17/11/2017 10:07 PM, 'Amitesh Sahay' via Django users wrote:
> Hello James,
>
> Thanks a lot for reply. There are couple of things that I would like
> to bring to the notice
>
> 1) I did go through that part of django document before posting the
> issue here.
> 2) In that doc, in the 2nd "if" condition, its talking about
> cleaned_data. In one of the public forum, I was asked to remove that
> part from my views.py, as I am not customizing my models.py. If I use
> cleaned_data. I entered the below in my earlier view.py
>
>             #username = form.cleaned_data.get('username')
>             #password = form.cleaned_data.get('password')
>             #email = form.cleaned_data.get('email')
>             #user.save()
>
> Please let me know if that was right? Also, can you point me to the
> exact changes that you want me to make in my views.py. I am sorry that
> I am asking this, but over the last 2 week or so, I am so frustrated
> that I am not able to think much on this part.
>
>
> Hello,
>
> Regards,
> Amitesh Sahay
>
> primary :: *91-907 529 6235*
>
>
>
> On Friday 17 November 2017, 12:04:25 PM IST, James Schneider
> <jrschn...@gmail.com> wrote:
>
>
>
>
> On Nov 15, 2017 8:32 AM, "'Amitesh Sahay' via Django users"
> <django...@googlegroups.com <mailto:django...@googlegroups.com>
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWNrWLhEzhM6649BsP5JByUXbsS7gvBVaKq2BkejKH7Rw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> .
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/1670974142.510163.1510916854178%40mail.yahoo.com
> <https://groups.google.com/d/msgid/django-users/1670974142.510163.1510916854178%40mail.yahoo.com?utm_medium=email&utm_source=footer>.

Amitesh Sahay

unread,
Nov 18, 2017, 3:28:13 AM11/18/17
to django...@googlegroups.com, Mike Dewhirst
Hello Mike, 

Thank you for your reply again. And I have already gone through the link. I am looking for exact code that I am supposed to write inside my 2nd "if" condition, as may be I am not able to understand the logic. Django docs have lots of information, which is creating confusion to me. The latest link that you have shared, it was helpful to some extent, and because of that I have progressed till now. But its a dead lock for me now. So, as I said, I need exact code that I am supposed to mention 

Hello,

Regards,
Amitesh Sahay

primary :: 91-907 529 6235




> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWNrWLhEzhM6649BsP5JByUXbsS7gvBVaKq2BkejKH7Rw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWNrWLhEzhM6649BsP5JByUXbsS7gvBVaKq2BkejKH7Rw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> .
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsub...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

James Schneider

unread,
Nov 18, 2017, 4:03:20 AM11/18/17
to django...@googlegroups.com

Here's how I would modify the view:


views.py
--------

def SignUpFormView(request):
    user_form = 'SignUpForm'
    template_name = 'test.html'

Delete the above two lines. It appears you are mixing class-based and function-based views. In this case, you are using a function-based view.


    if request.method == 'POST':
        form = user_form(request.POST)
Should be: form = SignUpForm(request.POST)


        if form.is_valid():
            form.save()
            #username = form.cleaned_data.get('username')
            #password = form.cleaned_data.get('password')
            #email = form.cleaned_data.get('email')
            #user.save()
You can remove these commented lines, it appears as though you were trying to build the user object manually. Your firm can likely do this for you.

            return render(request, template_name, {'form':form})

If the form is valid, then you should redirect to another page/view, not return to the same page. This is a common and almost required pattern with form submissions.

return HttpResponseRedirect('/signup_complete/')

The '/signup_complete/' should resolve to another page in your urls.py and usually displays a message saying that the user was successfully signed up. You can use any URL here that can be resolved by your urls.py.




    else:
        SignUpForm()
 This line just instantiates a new form object and then throws it away. It should be used in the event that request.method is not 'POST' and you generate a new, empty form for use in your template.

form = SignUpForm()



    return render(request, 'user_info/about.html')

You aren't passing the form object that you are creating to your template context, which is why it isn't rendering in your template.

return render(request, 'user_info/about.html', {'form': form})






test.html
---------

As far as your template goes, remove everything within the <form> definition and start with this:

<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" value="Submit">sign up </button>
</form>



Try these notifications and let me know how that works.

If you haven't already, I would try running through the Django tutorial, which covers how to handle forms and other components of Django.


-James
Reply all
Reply to author
Forward
0 new messages