如何让用户使用 django 在同一页面上登录和注册 [英] How to have user log in and registration on same page using django

查看:48
本文介绍了如何让用户使用 django 在同一页面上登录和注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个用户登录页面和一个用户注册页面,我如何才能在一个页面上同时拥有这两个页面?

Currently I have a user log in page and a user sign up page, how can I have both of these on one single page?

Base.html:

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

<body>
{%  block body %}

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

signup.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 %}

登录.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')

如果登录和注册由单独的视图处理,我该如何将它们放在一个页面上?预先感谢您的回复!我没有更多的细节要添加,它让我添加更多的细节,我为这个不必要的文字道歉.

How would I put both the log in and sign up on one page, if they are handled by separate views? Thank you in advance for your response! I have no more details to add and it is making me add more details i apologize for this unnecessary text.

推荐答案

在您需要在同一页面中使用多个表单的任何场景中,都可以应用以下技术.例如,目前您需要在同一页面上填写两个表单登录"和注册".

In any scenario where you need multiple forms in the same page the following technique can be applied. For example currently you need two forms 'Sign In' and 'Sign Up' on the same page.

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

这篇关于如何让用户使用 django 在同一页面上登录和注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆