2016-03-24 21 views
-1

Django kitabı ile Öğrenme Web Sitesi Geliştirme kullanıyorum.Django form hatası görüntüleyemiyor

Kaynak kodunu gullub'taki Source code yanlış yolundan indirdim. Uygulamayı windows makinemde çalıştırabiliyorum.

Ancak gelen giriş veya kayıt hataları sonrasında teslim içeren her nedense, bu hatalar html sayfası gösterilmeyenbulunmaktadır.

Lütfen sorunun ne olduğunu bana bildirin. Ben tarayıcı konsoluna gördüklerini Bir obeseration kod satırına ardından

hata mesaj görüntüler olduğunu

def register_page(request): 
    form = RegistrationForm(request.POST or None) 
    print request.method 
    if request.method == 'POST' and form.is_valid(): 
     user = User.objects.create_user(
      username=form.cleaned_data['username'], 
      password=form.cleaned_data['password1'], 
      email=form.cleaned_data['email'] 
     ) 
     return HttpResponseRedirect('/register/success/') 
    else: 
     form = RegistrationForm() 
    variables = RequestContext(request, {'form': form}) 
    print variables 
    return render_to_response('registration/register.html', variables) 

kayıt sayfası html kodu:

{% extends "base.html" %} 
{% block title %}User Registration{% endblock %} 
{% block head %}User Registration{% endblock %} 
{% block content %} 
    print "ddd" 
    {% if form.has_errors %} 
     <p>Your username and password didn't match. 
      Please try again.</p> 
    {% endif %} 

    <form method="post" action=""> 
     {% csrf_token %} 
     {{ form.as_p }} 
     <input type="submit" value="register" /> 
    </form> 
{% endblock %} 

Giriş sayfasını kodu:

{% extends "base.html" %} 
{% block title %}User Login{% endblock %} 
{% block head %}User Login{% endblock %} 
{% block content %} 
    {% if form.has_errors %} 
     <p>Your username and password didn't match. 
      Please try again.</p> 
    {% endif %} 
    <form method="post" action=""> 
     {% csrf_token %} 
     <p><label for="id_username">Username:</label> 
      {{ form.username }}</p> 
     <p><label for="id_password">Password:</label> 
      {{ form.password }}</p> 
     <input type="submit" value="login" /> 
     <input type="hidden" name="next" value="/" /> 
    </form> 
{% endblock %} 

form.py

import re 

from django.core.exceptions import ObjectDoesNotExist 
from django.contrib.auth.models import User 
from django import forms 


class RegistrationForm(forms.Form): 
    print forms.Form; 
    username = forms.CharField(
     label='Username', 
     max_length=30 
    ) 
    email = forms.EmailField(
     label='Email' 
    ) 
    password1 = forms.CharField(
     label='Password', 
     widget=forms.PasswordInput() 
    ) 
    password2 = forms.CharField(
     label='Password (Again)', 
     widget=forms.PasswordInput() 
    ) 

    # password validation: 
    def cleaned_password2(self): 
     # all valid values are accessible trough self.clean_data 
     if 'password1' in self.cleaned_data: 
      password1 = self.cleaned_data['password1'] 
      password2 = self.cleaned_data['password2'] 
      if password1 == password2: 
       return password2 
     raise forms.ValidationError('Passwords do not match.') 

    # username validation 
    def cleaned_username(self): 
     username = self.cleaned_data['username'] 
     if not re.search(r'^\w+$', username): 
      raise forms.ValidationError('Username can only contain alphanumeric characters and the underscore.') 
     try: 
      User.objects.get(username=username) 
     except ObjectDoesNotExist: 
      return username 
     raise forms.ValidationError('Username is already taken.') 


class BookmarkSaveForm(forms.Form): 
    url = forms.URLField(
     label='URL', 
     widget=forms.TextInput(attrs={'size': 64}) 
    ) 
    title = forms.CharField(
     label='Title', 
     widget=forms.TextInput(attrs={'size': 64}) 
    ) 
    tags = forms.CharField(
     label='Tags', 
     widget=forms.TextInput(attrs={'size': 64}) 
    ) 

http://localhost:8000/static/style.css kaynak yüklenemedi: sunucu 404 durumuyla cevap

+0

formu hataları görüntülemek için beklenen kod parçasına ihtiyacımız olurdu. –

+0

python2.7 ve django1.9.x kullanıyorum – niran

cevap

1

kod yanlıştır (Bulunamadı). Boş - - böyle şeyler ya yanlış olması durumunda formu oluşturulur bir çek içine method == 'POST' ve form.is_valid() birleştirerek

, yeni bir sağlama ediyoruz. Yapmaları gereken, yöntem "POST" ile eşit değilse yalnızca boş bir form oluşturmaktır; Form geçerli değilse, hataları içeren bu geçersiz formu şablona döndürmeleri gerekir.

Yani iki ayrı tablolar halinde olan, normal yolları takip etmek gerekir: Ben aynı problem var temele

def register_page(request): 
    if request.method == 'POST': 
     form = RegistrationForm(request.POST) 
     if form.is_valid(): 
      user = User.objects.create_user(
       username=form.cleaned_data['username'], 
       password=form.cleaned_data['password1'], 
       email=form.cleaned_data['email'] 
      ) 
      return HttpResponseRedirect('/register/success/') 
     else: 
      form = RegistrationForm() 
     variables = RequestContext(request, {'form': form}) 
+0

Öneriye göre değiştirdim ama hala hata görüntülenmiyor – niran

0

i did şeydir: kullanın self.add_error bir hataları takmak için formdaki belirli alan. Bu gibi) yerine forms.ValidationError (bir add_error() Çağrı:

şifre doğrulaması:

def cleaned_password2(self): 
     # all valid values are accessible trough self.clean_data 
     if 'password1' in self.cleaned_data: 
      password1 = self.cleaned_data['password1'] 
      password2 = self.cleaned_data['password2'] 
      if password1 == password2: 
       return password2 
     msg = 'Passwords do not match.' 
     self.add_error('password1',msg) 
+0

eklenen formlar py kodu soruya – niran