简体   繁体   中英

Adding User Registration to Django App

I'm following this tutorial and finished it: http://tutorial.djangogirls.org/en/django_forms/README.html

I want to continue and add a register button next to the login button which I did, but I couldn't get it to work. I tried doing action = "{% url blog.views.register %}" but I kept getting the error about no arguments found. I would post all my code but I'm almost sure it's way off base. I followed this idea: http://tea.cesaroliveira.net/archives/460 but I couldn't get it to work right. What is the best way to implement a registration form? Can someone give me an example or a guide, even just a short one written out to the approach I should take to add registration features. I had added a second form in login.html template with method=post and action = "{% url blog.views.register %}" with another template for registration, register.html

in blog/views.py

def register(request):
    if request.method == "POST":
        form = UserCreateForm(request.POST, instance=post)
    return render(request, 'blog/register.html)

and in forms.py

from django import forms
from .models import Post
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ( "username", "email" )\

and in blog/urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from blog import views
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/login/$', 'django.contrib.auth.views.login'),
    url(r'^accounts/logout/$', 'django.contrib.auth.views.logout', {'next_page': '/'}),
    url(r'', include('blog.urls')),
    url(r'^accounts/register/$', views.register , {'next_page': '/'}),
)

As far as validation I can worry about that later, I just want to get it to work so that they can click register and bring up a registration template with a form to fill out. I'm not sure if this requires a new model or not as django has user capabilities built in already.

Any help/advice would be much appreciated. I just want to see what is wrong with my approach and what a better approach would be.

Alright, starting from the top and this is most likely the first reason why your code is probably falling apart, this {% url blog.views.register %} is wrong. Per the Django docs on reverse urls :

In order to perform URL reversing, you'll need to use named URL patterns.

So, to create a reverse url for the registration view, your urlpattern should look like this:

urlpatterns = patterns('',
    url(r'^accounts/register/$', views.register , name="register"),
)

I took out the 'next' item because you can redirect from the view. And in the template, you put:

{% url 'blog:register' %} , the left hand argument being the app name and the right-hand arg being the name of the url pattern.

Second, I'm not sure how you're handling the login form, but the register view is expecting an instance that doesn't exist. Instance is used when you're UPDATING an existing object. For your registration, that's not what's happening, so it doesn't need to be there.

def register(request):
    if request.method == "POST":
        form = UserCreateForm(request.POST) # filled form/i'm skipping validation for this example
        return HttpResponseRedirect('/') # go to some other page if successfully saved
    else:
        form = UserCreateForm # if the user accessed the register url directly, just display the empty form
    return render(request, 'blog/register.html',  {'form': form})

But ultimately, I highly recommend django-allauth for user registration/profile stuff.

UPDATE : Fixed the reverse url for register.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM