简体   繁体   中英

Django User Logging In System | Views.py doesn't work

I am currently making a user authentication system. I have a login screen with a couple of input fields. I can log the values of the input fields to the console in my javascript. I send the data to my backend. Submitting doesn't work and in my views.py loggin the data to the console too. I can basically completely remove the views.py function and the Template is shown anyway, Is that because I am using the Django Authentication Urls? so the registration folder is the default for the django registration? How can I send that to my backend? I have my logging in template ( https://pastebin.com/RKe5ECps )

Views.py

def login_user(request):
    if request.method == "POST":
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('forum')
        else:
            messages.success(request, ("There was an error Loggin in, Try Again!"))
            return redirect('login')
    else:
        return render(request, 'login.html', {})

Urls.py

path('', include('django.contrib.auth.urls')),

Folder Structure: templates/registration/login.html templates/registration/signup.html

I tried removing the authentication, but than i couldn't see anything, because at the login/ url there was nothing.

Yes, you have not linked your view in your urls. So now you are using the built-in views in django.contrib.auth you need to explicitly add your view.


urlpatterns = [
    ...,
    path("login/", login_user, name="login"),
    ...
]

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