2017-03-18 86 views
0

好的,所以我的註冊工作正常。我有一個錯誤的地方,是trowing以下頁面未找到404點擊登錄按鈕時

Request Method: POST 
Request URL: http://localhost:8000/users/%7Burl%20'users:login'%20%25%7D 
Using the URLconf defined in loginReg.urls, Django tried these URL patterns, in this order: 
^users/ ^$ [name='index'] 
^users/ ^register$ [name='register'] 
^users/ ^success$ [name='success'] 
^users/ ^login$ [name='login'] 
^users/ ^logout$ [name='logout'] 
The current URL, users/{url 'users:login' %}, didn't match any of these. 

的錯誤,當我拿出登錄部分,所以我傾向於認爲瘦了way.I包括models.py的URL形式的其餘部分的工作。 py和兩個html的部分我希望有人可以看到我錯過的錯誤。

指數HTML

<!DOCTYPE html> 
<html> 
<head> 
    <title>Login and Registration</title> 
</head> 
<body> 
    {% if messages %} 
     {% for message in messages %} 
      <p>{{ message }}</p> 
     {% endfor %} 
    {% endif %} 
    <div class="register"> 
     <h1>Register</h1> 
     <form class="" action="{% url 'users:register' %}" method="post"> 
      {% csrf_token %} 
      <p>First Name: <input type="text" name="first_name" value=""></p> 
      <p>Last Name: <input type="text" name="last_name" value=""></p> 
      <p>Email: <input type="text" name="email" value=""></p> 
      <p>Password: <input type="Password" name="password" value=""></p> 
      <p>Confirm Password: <input type="password" name="confirm_password" value=""></p> 
      <input type="submit" name="" value="Register"> 
     </form> 
    </div> 
    <div class="login"> 
     <h1>Login</h1> 
     {% csrf_token %} 
     <form class="" action="{url 'users:login' %}" method="post"> 
     <p>Email: <input type="text" name="email" value=""></p> 
     <p>Password: <input type="Password" name="password" value=""></p> 
     <input type="submit" name="" value="Login"> 
     </form> 
    </div> 
</body> 
</html> 

Models.py

from __future__ import unicode_literals 
from django.db import models 
import re, bcrypt 
EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+_-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z]+$') 

class UserManager(models.Manager): 
    def add_user(self, postData): 
     errors = [] 
     if not len(postData['first_name']): 
      errors.append('First name is required') 
     if len(postData['last_name']) < 2: 
      errors.append('Last name must be at least 2 characters long!') 
     if not len(postData['email']): 
      errors.append('Email is required!') 
     if not EMAIL_REGEX.match(postData['email']): 
      errors.append('Please enter a valid email!') 
     check_email = self.filter(email = postData['email']) 
     if check_email: 
      errors.append('Sorry email already exist!') 
     if len(postData['password']) < 8: 
      errors.append('Password must be at least 8 characters!') 
     if not postData['password'] == postData['confirm_password']: 
      errors.append('Passwors must match!') 

     modelsResponse = {} 



     if errors: 
      # failed validations 
      modelsResponse ['isRegistered'] = False 
      modelsResponse['errors'] = errors 
     else: 
      # passed validations, create a new user 
      hashed_password = bcrypt.hashpw(postData['password'].encode(), bcrypt.gensalt()) 
      user = self.create(first_name = postData['first_name'] , last_name = postData['last_name'], password = hashed_password) 
      modelsResponse ['isRegistered']= True 
      modelsResponse['user'] = user 

     return modelsResponse 

     def login_user(self, postData): 
      user = self.filter(email = postData['email']) 
      errors = [] 
      modelsResponse = {} 
      if not user: 
       #invalid email 
       errors.append('Invalid email!') 

      else: 
       #found a user match, check the password to see if they match 
       if bcrypt.checkpw(postData['password'].encode(), user[0].password.encode()): 
        #login the user 
        modelsResponse['isLoggedIn'] = True 
        modelsResponse['user'] = user[0] 
       else: 
        # invalid email password combination 
        errors.append('Invalid email/password combination!') 

      if errors: 
       modelsResponse['isLoggedIn'] = False 
       modelsResponse['errors'] = errors 

     return modelsResponse 

class User(models.Model): 
    first_name = models.CharField(max_length = 50) 
    last_name = models.CharField(max_length = 50) 
    email = models.CharField(max_length = 100) 
    password = models.CharField(max_length =100) 
    created_at = models.DateTimeField(auto_now_add = True) 
    updated_at = models.DateTimeField(auto_now = True) 

    objects = UserManager() 

Urls.py

從django.conf.urls輸入網址,包括 。進口觀點

urlpatterns = [ 
    url(r'^$', views.index, name= 'index'), 
    url(r'^register$', views.register, name = 'register'), 
    url(r'^success$', views.success, name = 'success'), 
    url(r'^login$', views.login, name = 'login'), 
    url(r'^logout$', views.logout, name = 'logout'), 
] 

views.py

from django.shortcuts import render, redirect 
from . models import User 
from django.contrib import messages 

# Create your views here. 
def index(request): 
    return render(request, 'logReg/index.html') 

def register(request): 
    viewsResponse = User.objects.add_user(request.POST) 
    if viewsResponse['isRegistered']: 
     request.session['user_id'] = viewsResponse['user'].id 
     request.session['user_fname'] = viewsResponse['user'].first_name 
     return redirect('users:success') 
    else: 
     for errors in viewsResponse['errors']: 
      messages.error(request, errors) 
     return redirect('users:index') 


def success(request): 
    if 'user_id' not in request.session: 
     messages.error(request, 'Must be logged in!') 
     return redirect('users.index') 
    return render(request, 'logReg/success.html') 

def login(request): 
    viewsResponse = User.objects.login_user(request.POST) 
    if viewsResponse['isLoggedIn']: 
     request.session['user_id'] = viewsResponse['user'].id 
     request.session['user_fname'] = viewsResponse['user'].first_name 
     return redirect('users:success') 
    else: 
     for errors in viewsResponse['errors']: 
      messages.error(request, errors) 
     return redirect('users:index') 

def logout(request): 
    return redirect('users.index') 

回答

1

你有一個錯字

{url 'users:login' %} 

應該

{% url 'users:login' %} 
#^ missing % 
+0

謝謝你,我知道是愚蠢的東西,我需要第二一對可愛的。我可以在兩分鐘內接受答案 –

相關問題