[英]Django UserCreationForm on homepage
I'm trying to create a usercreationform on the homepage of my website. 我正在尝试在网站首页上创建一个usercreationform。 After reading and watching tutorials on user creation I noticed everyone creates a separate HTML page for "signup", however, I want my signup page to be directly on my homepage - is this a possibility?
在阅读并观看了有关用户创建的教程之后,我注意到每个人都为“注册”创建了单独的HTML页面,但是,我希望我的注册页面直接位于我的主页上-这可能吗? I'm finding it difficult to understand with 'accounts' having its own separate app, as well as the homepage having its own app, which I have called mine 'game'.
我发现很难通过拥有自己独立应用程序的“帐户”以及拥有自己独立应用程序的首页来理解,我称之为“游戏”。 Do both apps have to be separate?
两个应用程序必须分开吗? Am I able to make the accounts app my main 'homepage' app?
我可以将帐户应用程序设为主要的“主页”应用程序吗?
Can anyone recommend any tutorials on this? 谁能推荐有关此的任何教程? I think I should also mention I'm quite new to django.
我想我也应该提到我对django很陌生。 Thank you.
谢谢。
My homepage app (titled game) urls.py: 我的首页应用(标题游戏)urls.py:
from django.contrib import admin
from django.urls import path
from.import views
urlpatterns = [
path('', views.game_start),
]
views.py: views.py:
from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
from .models import Game
def game_start(request):
games = Game.objects.all().order_by('date') # grabs all records in game in db table, order by date
return render (request, 'game/game_start.html', {'games':games})
def signup_view(request):
form = UserCreationForm()
return render(request, 'game/game_start.html', {'form': form})
accounts/urls.py: 账户/ urls.py:
from django.conf.urls import url
from .import views
app_name = 'accounts'
urlpatterns = [
path('', game_views.game_start, name="home"),
]
accounts/views.py: 账户/ views.py:
from django.http import HttpResponse
from django.shortcuts import render
def about(request):
# return HttpResponse('Price is right game one')
return render(request, 'about.html')
I want my signup page to be directly on my homepage - is this a possibility?
我希望我的注册页面直接在我的主页上-这可能吗?
Yes it's a possibility that you can define a custom signup
function in your accounts app and then import that inside of your homepage app like this: 是的,您可以在帐户应用程序中定义自定义
signup
功能,然后将其导入到首页应用程序中,如下所示:
accounts/views.py: 账户/ views.py:
def signup(request):
data = {'form':None, 'user_created': False}
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
# do soemthing with the registered user
data['user_created'] = True
else:
form = UserCreationForm()
data['form'] = form
return data
homepage/views.py: 主页/ views.py:
from accounts.views import signup
def game_start(request):
games = Game.objects.all().order_by('date')
data = {'msg': ''}
response = signup(request)
if response['user_created']:
# you can redirect user here to somewhere else when they have been registered.
data['msg'] = 'Thanks for being the part of our beautiful community!'
return render(request, 'game/game_start.html', {
'games':games,
'form': response['form'],
'msg': data['msg']
})
game_start.html: game_start.html:
<p>{{msg}}</p>
<form action="/" method="post">
{% csrf_token %}
{{ form }}
<button type="submit">Sign Up</button>
</form>
Do both apps have to be separate?
两个应用程序必须分开吗?
Well, you can have both of them under one app but that is not recommended because of the following reasons: 好吧,您可以将它们都放在一个应用程序中,但由于以下原因,我们不建议这样做:
If you are having difficult understanding what apps in django are, then you can simply take a look at my answer here 如果您难以理解django中的应用程序,那么您可以在这里简单地查看我的答案
You could include the form in your "game_start.html" template: 您可以在“ game_start.html”模板中包含该表单:
{% if not user.is_authenticated %}
<form role="form"
action="{% url 'player_login' %}"
method="post">
{% csrf_token %}
<p>Please login.</p>
{{ form.as_p }}
<button type="submit">Sign in</button>
</form>
{% endif %}
This assumes you have a named url pattern player_login
. 假设您有一个命名的URL模式
player_login
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.