2016-07-19 9 views
0

からのログイン「homepage login form Djangoホームページサイトのメインホームページに直接ログインフォームを置くために、私はこのページ の指示に従っジャンゴ

、 ではなく、ログインと登録などのために別のページを持ちます、上記

が、リンクはので、私はフォームが表示されるようにするか見当がつかないviews.pyとbase.html

は表示されません。

あなたがライブサイトに行けば:http://mtode.com

それはIDとパスワードのフォームを行方不明だと私は投稿内容そのpostformある他のフォームに入力しようとするときも、それは私にエラーがスローされます。

私が入れた後、私は何を使用するためにこれらのコードを入れて持っている理由はわかりませんが、これはviews.py

def login(request): 
     username = request.POST['username'] 
     password = request.POST['password'] 
     user = authenticate(username=username, password=password) 
     if user is not None: 
      if user.is_active: 
       login(request, user) 
       redirect_url = '/' 
      else: 
       messages.error(request, "Not Successfully Created") 
     else: 
      messages.success(request, "Successfully Created") 

@login_required 
def post_list(request): 
    form = PostForm(request.POST or None) 
    if form.is_valid(): 
     instance = form.save(commit=False) 
     print (form.cleaned_data.get("title")) 
     instance.save() 
     # message success 
     messages.success(request, "Successfully Created") 
     return HttpResponseRedirect(instance.get_absolute_url()) 
    #else: 
     #messages.error(request, "Not Successfully Created") 
    queryset = Post.objects.all()#.order_by("-timestamp") 
    context = { 
     "object_list": queryset, 
     "title": "List", 
     "form": form, 
    } 
    return render(request, "post_list.html", context) 

urls.py

url(r'^login/$', 'django.contrib.auth.views.login', name='login'), 
url(r'^logout/$', 'django.contrib.auth.views.logout', name='logout') 

(あります次のコードは、リダイレクトのsettings.pyにあります)

settings.py

LOGIN_URL = reverse_lazy('login') 
LOGIN_REDIRECT_URL = reverse_lazy('home') 

、最終的には

{% extends "base.html" %} 
    {% block content %} 
     {% if form.errors %} 
      <p>Your username and password didn't match. Please try again.</p> 
     {% endif %} 

     {% if next %} 
      {% if user.is_authenticated %} 
       <p>Your account doesn't have access to this page. To proceed, 
       please login with an account that has access.</p> 
      {% else %} 
      <p>Please login to see this page.</p> 
      {% endif %} 
     {% endif %} 

    <form method="post" action=""> 
    {% csrf_token %} 
    <table> 
    <tr> 
     <td>{{ form.username.label_tag }}</td> 
     <td>{{ form.username }}</td> 
    </tr> 
    <tr> 
     <td>{{ form.password.label_tag }}</td> 
     <td>{{ form.password }}</td> 
    </tr> 
    </table> 

    <input type="submit" value="login" /> 
    <input type="hidden" name="next" value="{{ next }}" /> 
    </form> 

    {# Assumes you setup the password_reset view in your URLconf #} 
    <p><a href="">Lost password?</a></p> 

    <div class='two columns right mgr'> 
     <h1>Form</h1> 
    <form method='POST' action=''>{% csrf_token %} 
    {{ form.as_p }} 
    <input class="button-primary" type='submit' value='Create Post' /> 
    </form> 
    </div> 

    <div class='four columns left'> 
     <h1>{{ title }}</h1> 

    {% for obj in object_list %} 
    <div class="row"> 
     <div> 
     <a href='{{ obj.get_absolute_url }}'> 
     <div class="thumbnail"> 
      <!--<img src="..." alt="...">!--> 
      <div class="caption"> 
      <h3>{{ obj.title }}<small> {{ obj.timestamp|timesince }} ago</small></h3> 
      <p>{{ obj.content|linebreaks|truncatechars:120 }}</p> 
      <!-- <p><a href='{{ obj.get_absolute_url }}' class="btn btn-primary" role="button">View</a> </p>--> 
      </div> 
     </div></a> 
     </div> 
     <hr /> 
    </div> 

    {% endfor %} 

    </div> 
    {% endblock content %} 

答えて

0

あなた最初の形式は、セットアップの権利ではありませんpost_list.html。一つの解決策はhtisのような何かをすることができます:

<form action="/login/?next=/home/" method="post"> 
    {% csrf_token %} 
     <input type="text" placeholder="Username" name="username"> 
     <input type="password" placeholder="Password" name="password"> 
    <button type="submit">Sign in</button> 
    </form> 

アクションのURLがログインに行くために、次のリンクは、ログイン後に復帰位置になります。

関連する問題