2016-07-18 8 views
0

私はDjangoの初心者です。私が作成したフォームはちょっとしたトラブルを与えています。投稿後にDjangoフォームをリダイレクトすることはできません

ユーザー認証とログインのためにDjangoでフォームを作成しました。しかし、フォームは私が提出した後に指定したリンクにリダイレクトされません。これは、の認証の機能がviews.pyにあるため、ユーザーが「なし」になっているためです。しかし、ユーザーがデータベースに存在するため、これは起こりません。 Djangoの内部開発者サーバーであるhttp://127.0.0.1:8000/admin/に行って確認しました。

リダイレクトしようとしているURLが存在します。そこに問題はありません。これに関連し Pythonのファイルは、以下のとおりです。

forms.py:

from django.contrib.auth.models import User 
from django import forms 


class UserForm(forms.ModelForm): 


    class Meta: 
     model = User 
     fields = ['username', 'email', 'password'] 

views.py:

私は次のコード内のすべての輸入を省略しました。そこに問題はありません。

{% extends 'music/base.html' %} 
{% block title%}Registration Form {% endblock %} 

{% block body %} 
{% load staticfiles %} 
<link rel="stylesheet" type="text/css" href="{% static 'music/index_style.css' %}" /> 

<div class="block"> 
    <form action="" method="post" > 
     {% csrf_token %} 
     <fieldset> <!-- Gives it a better look by putting a heading and background around the form --> 
      <legend>Create a new account:</legend><!-- This the common heading for the form --> 
      {% include 'music/form_template.html' %} 
      <br> 
      <input type="submit" value="Submit"> 
     </fieldset> 
    </form> 
</div> 
{% endblock %}> 

登録フォームテンプレートに含まform_template.htmlは次のとおりです:また

{{ form.non_field_errors }} 
{{ form.errors }} 
{{ form.as_p }} 

、もうひとつの正常ではありません

class UserFormView(View): 
    form_class = UserForm 
    template_name = 'music/registration_form.html' 

    def get(self, request): # This function is executed if the server obtains a GET request. 
     form = self.form_class(None) 
     return render(request, self.template_name, {'form': form}) 
    ''' 
    The server gets a GET request every time a new user wants to register, i.e they want an empty form. 
    That's why we pass None in the function above. 
    It gets a POST request every time a filled form is submitted. 
    ''' 

    def post(self, request): # This function is executed if the sever recevies a POST request. 
     form = self.form_class(request.POST) 

     if form.is_valid(): 
      user = form.save(commit=False) # This creates an object, but does not save it to the database. 
      # Therefore, we can do some changes. 

      username = form.cleaned_data['username'] 
      password = form.cleaned_data['password'] 
      # Here, cleaned_data is converted data to suitable format. Like the date entered is converted to a 
      # suitable format as its format is different all around the world. 
      # Now you can change the username by user.username = 'some_name' or you can change the password by 
      # user.set_password(new_password) 

      user.save() # This line of code actually saves the code. 

      user = authenticate(username=username, password=password) 
      # This checks if the user actually exists in the database. 

      if user is not None: 
       if user.is_active: # This if the user is not banned or anything like that. 
        login(request, user) # This logs in the user. 
        return redirect('music:index') # Redirects the user to index page. 
      else: 
       return render(request, self.template_name, {'form': form}) 

     else: 
      form = self.form_class(None) 
      return render(request, self.template_name, {'form': form}) 
    # This returns the filled form again if the the form is not valid. 

登録フォームテンプレートは(htmlファイル)であります上記のフォームを使用して新しいユーザーを作成すると、そのユーザーのパスワードは次のように表示されます:

"無効なパスワード形式または不明なハッシングアルゴリズムです。"

これは、現在のユーザーを編集できるhttp://127.0.0.1:8000/admin/で見ることができます。

答えて

0

ここにいくつかの問題があります。

をcommit = Falseで正しく呼び出すと、(コメント状態として)正しく保存する前にハッシュされたパスワードを設定できるようになりますが、決して実際に行っていないので、ユーザーはハッシュされていないパスワードで保存されます。これは決して検証されません。

同様に、ユーザーのis_activeプロパティを決して設定しないので、少し下のチェックは常に失敗します。

user = form.save(commit=False) 
password = form.cleaned_data['password'] 
user.set_password(password) 
user.is_active = True 
user.save() 
+0

is_activeを手動で設定する必要があるかどうかわかりませんでした。出来た!! –

関連する問題