2017-08-09 89 views
0

目前我有一個用戶登錄頁面和一個用戶註冊頁面,我怎樣才能在單個頁面上顯示這兩個頁面?如何讓用戶使用django登錄並在同一頁面上註冊

base.html文件:

<!doctype html> 
<head> 
    {% block head %} 
    <title>base</title> 
    {% endblock %} 
</head> 

<body> 
{% block body %} 

{% endblock %} 
</body> 
</html> 

signup.html:

{% extends 'core/base.html' %} 

{% block head %} 
    <title> Sign Up</title> 
{% endblock %} 
{% block body %} 
    <h2>Sign up</h2> 

    <form method="post"> 
    {% csrf_token %} 
    {{ form.as_p }} 
    <button type="submit">Sign up</button> 
    </form> 
{% endblock %} 

的login.html:

{% extends 'core/base.html' %} 

{% block head %} 
    <title> Login</title> 
{% endblock %} 

{% block body %} 
<h1>Login</h1> 

<form method = 'post'> 
    {% csrf_token %} 
    {{ form.as_p }} <!--'form' comes from login view imported in urls--> 
    <button type = 'submit'>Login</button> 
</form> 

{% endblock %} 

urls.py:

from django.conf.urls import url 
from django.contrib import admin 
from django.contrib.auth.views import login 
from core import views as core_views 


urlpatterns = [ 
    url(r'^$', core_views.login_redirect, name = 'login_redirect'), 
    url(r'^admin/', admin.site.urls), 
    url(r'^login/$', login, {'template_name': 'core/login.html'}, name='login'), 
    url(r'^signup/$', core_views.signup, name='signup'), 
    url(r'^account/$', core_views.account_page, name = 'account_page') 

] 

views.py:

from django.shortcuts import render 

from django.contrib.auth import login, authenticate 
from django.contrib.auth.forms import UserCreationForm 
from django.shortcuts import render, redirect 
from django.http import HttpResponse 
from django.contrib.auth.forms import UserCreationForm 

def login_redirect(request): 
    return redirect('login') 



def signup(request): 
    if request.method == 'POST': 
     form = UserCreationForm(request.POST) 
     if form.is_valid(): 
      form.save() 
      return redirect('/account') 
    else: 
     form = UserCreationForm() 

     args = {'form': form} 
     return render(request, 'core/signup.html', args) 


def account_page(request): 
    return HttpResponse('success') 

我將如何把兩者在日誌中,並簽署了一個頁面上,如果它們是由不同的看法如何處理?預先感謝您的迴應!我沒有更多的細節要添加,它讓我添加更多細節,我爲這些不必要的文本道歉。

+0

你能削減你的代碼只有相關的部分?此外,這可能是一個更好的問題https://codereview.stackexchange.com/ – WookieCoder

回答

2

在任何情況下,如果您需要同一頁面中的多個表單,則可以應用以下技術。 例如,目前您需要在同一頁面上使用兩種表單「登錄」和「註冊」。

的index.html

<!-- Sign In Form --> 
<form> 
    <button type='submit' name='submit' value='sign_in'></button> 
</form> 
<!-- Sign Up Form --> 
<form> 
    <button type='submit' name='submit' value='sign_up'></button> 
</form> 

views.py

def index(request): 
    if request.method == "POST": 
     if request.POST.get('submit') == 'sign_in': 
      # your sign in logic goes here 
     elif request.POST.get('submit') == 'sign_up': 
      # your sign up logic goes here 
相關問題