0

I keep getting the message and have tried/ made sure:

  1. My browser is accepting cookies

  2. That the view function passes a request to the template's render method in the Views.py file:

Views.py File ##

**from django.shortcuts import render, redirect**
from django.contrib.auth.models import User
from django.contrib import auth

def signup(request):
    if request.method == 'POST':
        # User has info and wants an account now!
        if request.POST['password1'] == request.POST['password2']:
            try:
                user = User.objects.get(username=request.POST['username'])
                return render(request, 'accounts/signup.html', {'error':'Username has already been taken'})
            except User.DoesNotExist:
                user = User.objects.create_user(request.POST['username'], password=request.POST['password1'])
                auth.login(request,user)
                return redirect('home')
        else:
            return render(request, 'accounts/signup.html', {'error': 'Passwords must match'})
    else:
        # User wants to enter info
        return render(request, 'accounts/signup.html')

def login(request):
    if request.method == 'POST':
        user = auth.authenticate(username=request.POST['username'],password=request.POST['password'])
        if user is not None:
            auth.login(request, user)
            return redirect('home')
        else:
            return render(request, 'accounts/login.html', {'error': 'Usename or password is invalid'})
    else:
        return render(request, 'accounts/login.html')

def logout(request):
    if request.method == 'POST':
        auth.logout(request)
        return redirect('home')
    # Need to route to home page
    return render(request, 'accounts/signup.html')
  1. I have added {% csrf_token %} in the template for inside each POST form that targets an internal URL:

  <form class="form-signin" method="POST" action="{% url 'signup' %}">
{% csrf_token %}
<input class="form-control" placeholder="Username" required autofocus type="text" name="username" />
<input class="form-control" placeholder="Password" required autofocus type="password" name="password1" />
<input class="form-control" placeholder="Confirm Password" required autofocus type="password" name="password2" />
<br/>
<br/>
<button class="btn waves-effect waves-light" type="submit">Sign UP!</button>
<br/>
  </form>
  1. I have made sure CsrfViewMiddleware is in my settings file:

    MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]

Please help! Thank you.

GoodKids
  • 11
  • 3
  • Show us what url you're accessing, and that url's definition in `urls.py`. – John Gordon Jun 17 '20 at 15:56
  • from django.urls import path, include from . import views urlpatterns = [ path('signup', views.signup, name='signup'), path('login', views.login, name='login'), path('accounts/logout/', views.logout, name='logout'), ] – GoodKids Jun 17 '20 at 16:23
  • What url address are you accessing in the browser? – John Gordon Jun 17 '20 at 16:29
  • I have been using the localhost to connect. Maybe i am not understanding your question correctly? – GoodKids Jun 17 '20 at 16:35
  • after local host i am going to /accounts/signup – GoodKids Jun 17 '20 at 16:36
  • Hostname is irrelevant, but I wanted to see the url path after the hostname, i.e. `http://localhost/my/path/to/url` – John Gordon Jun 17 '20 at 16:37
  • Maybe the view is using some other form template without you realizing it? Try adding some text to the form definition, i.e. something like `

    This is the right form

    `, and then verify that the text appears on the form.
    – John Gordon Jun 17 '20 at 16:41
  • Also print `csrf_token` in the form as plain text, so you can see if it's blank. – John Gordon Jun 17 '20 at 16:42
  • Yep that seems to be working... I am really stumped – GoodKids Jun 17 '20 at 16:44
  • Everything else seems to be working with the form. when i hit sign up i get: Forbidden (403) - CSRF verification failed. Request aborted. – GoodKids Jun 17 '20 at 16:46

0 Answers0