How to have user log in and registration on same page using django

北慕城南 提交于 2020-07-02 11:27:22

问题


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:

{% 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')

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.


回答1:


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


来源:https://stackoverflow.com/questions/45598916/how-to-have-user-log-in-and-registration-on-same-page-using-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!