#Beginner - Django Register/authentication

17 messages ยท Page 1 of 1 (latest)

topaz shard
#

app/views.py

def auth_register(request):
    form = RegisterForm()
    if request.method == 'POST':
        if form.is_valid():
            form = RegisterForm(request.POST)
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = User.objects.create_user(username=username,password=password)
            print('User created')
        return redirect('task list')

    return render(request,'register.html',{'form':form})

app/templates/register.html

<!DOCTYPE html>
<html>
    <head>
        <title>
            Register
        </title>
    </head>

    <body>
        <form method="post" action="">
            {% csrf_token %}
            {{form}}
            <input type="submit" value="register">
        </form>
    </body>
</html>

app/forms.py

from .models import Task
from django.forms import ModelForm
from django import forms
class TaskForm(ModelForm):
    class Meta:
        model = Task
        fields = ['task_name']

class RegisterForm(forms.Form):
    username = forms.CharField(max_length=15)
    password = forms.CharField(widget=forms.PasswordInput)

class LoginForm(forms.Form):
    username = forms.CharField(max_length=15)
    password = forms.CharField(widget=forms.PasswordInput)
    
    ```
app/url.py
```py
from . import views
from django.urls import path

urlpatterns = [
    path('home/',views.homepage,name='home'),
    path('task/',views.task_form),
    path('show-tasks/',views.task_list,name='task list'),
    path('register/',views.auth_register)
]
#

I try to create a user on the form and when i check out the admin site,it doesn't show that a new user is created

#

Thanks folks for taking ur time reading this,really appreciate any help i can get :))

full helm
#

To bypass your question - is there a reason you are not using the supplied views for this?

topaz shard
#

Supplied views?

mild inlet
#

Putting aside the fact that you shouldn't reinvent the wheel, since this level of auth is baked already into the framework. There's a critical branch that's missing in your view:

def auth_register(request):
    form = RegisterForm()
    if request.method == 'POST':
        if form.is_valid():
            form = RegisterForm(request.POST)
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = User.objects.create_user(username=username,password=password)
            print('User created')
+      else:
+           # What happens if the form isn't valid?
+           return render(request,'register.html', {'form':form})
        return redirect('task list')

    return render(request,'register.html',{'form':form})
full helm
mild inlet
#

You also need to consider that the registration view doesn't automatically log the user in. So your redirect('task list') ||Can you even have spaces in name? ๐Ÿค”|| will not really take you to the user's task list.

topaz shard
topaz shard
full helm
#

Well, technically you were not, django.auth does not appear to supply a registration view. Just all the others.

mild inlet
full helm
#

Possibly because just putting up a registration view whitout thinking about bot protection etc it a bad idea

topaz shard
#

I see

#

Ohhh i am populating the form after checking if its valid. Thats why it doesn't work. ๐Ÿ˜”

full helm
#

Ops. Also that, yes. ๐Ÿ˜