我正在django制作一个网站,在我的主页上,我想显示我最近的博客文章列表,下面几个街区我想制作一个简单的联系表格。博客和联系表单独工作正常。但我希望将它们包含在同一页面中(显然在同一个网址中)。
views.py是:
from .forms import NameForm
def get_name(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = NameForm()
return render(request, 'personal/index.html', {'form': form})
如果你想查看forms.py,那么:
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
我主页的urls.py中的urlpattern是:
urlpatterns = [
url(r'^$', ListView.as_view(
queryset=Post.objects.all().order_by("-date")[:2],
template_name="personal/index.html")),
url(r'^$', views.get_name, name='contact'),
]
使用此urlpatter,博客文章列表完美显示但联系表单未显示。但是,下面的urlpattern联系表单显示但博客文章没有显示。
urlpatterns = [
url(r'^$', views.get_name, name='contact'),
url(r'^$', ListView.as_view(
queryset=Post.objects.all().order_by("-date")[:2],
template_name="personal/index.html")),
]
我想让这两个内容显示在同一页面中。请帮我。如果您需要更多信息,请告诉我们。
答案 0 :(得分:0)
同一网址不可能有多个视图。每个网址只能有一个视图。
在这种情况下,最简单的解决方法是更新您的get_name
视图,使其包含模板上下文中的帖子。
def get_name(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
return HttpResponseRedirect('/thanks/')
else:
form = NameForm()
post_list = Post.objects.all().order_by("-date")[:2]
return render(request, 'personal/index.html', {'form': form, 'post_list': post_list})
然后删除使用ListView
的网址格式,以便get_name
处理请求。
答案 1 :(得分:0)
虽然您不能直接执行此操作,但是您可以在views.py中考虑您的函数以某种方式处理请求,即当它从一个模型收到发布请求时,仅初始化相应模型的表单。为此,您需要为按钮分配一个名称。参见下面的代码-
def SignUpLogin(request):
message = ""
Login_message = ""
print(request.POST)
if request.method == 'POST':
if request.POST.get('UserForgotPassword') == 'Sign Up':
form_SignUP_Login = LoginForm()
print("POST request received")
print(request.POST)
form_SignUP=SignUpForm(request.POST)
print(form_SignUP)
if form_SignUP.is_valid():
if request.POST["Password"] != request.POST["Confirm_Password"]:
message = "* Passwords do not match"
#raise forms.ValidationError(('Passwords do not match'), code="PasswordNotMatched")
else:
try:
user = User.objects.get(username=request.POST["Username"])
context= {'form': form_SignUP, 'error':'* Username already taken. Please try different username.'}
return render(request, 'User/SignUp.html', context)
except User.DoesNotExist:
user = User.objects.create_user(request.POST["Username"], request.POST["Email"], request.POST["Password"])
user.first_name = request.POST["First_Name"]
user.last_name = request.POST["Last_Name"]
user.save()
form_SignUP = SignUpModel(Username = request.POST["Username"], First_Name = request.POST["First_Name"], Last_Name = request.POST["Last_Name"], Email = request.POST["Email"], Company_Name = request.POST["Company_Name"], Address = request.POST["Address"], Password = make_password(request.POST["Password"]), Confirm_Password = make_password(request.POST["Confirm_Password"]), Phone_Number = request.POST["Phone_Number"])
form_SignUP.save()
#queryset = SignUpModel.objects.get(Username = request.POST["Username"])
#queryset.Password = "pwd_removed"
#queryset.Confirm_Password = "pwd_removed"
#queryset.save()
#send_email_to_host(request.POST["First_Name"], request.POST["Family_Name"], request.POST["Number_Of_Adults"], request.POST["Number_Of_Kids"], request.POST["Email"])
return redirect(HomePage)
elif request.POST.get('UserLogin') == 'Login':
form_SignUP = SignUpForm()
form_SignUP_Login=LoginForm(request.POST)
if form_SignUP_Login.is_valid():
user = authenticate(username=request.POST["Username"], password=request.POST["Password"])
if user is not None:
print("User authenticated")
return redirect(HomePage)
else:
print("User not authenticated")
form_SignUP_Login = LoginForm()
Login_message = "Username and password combination is not correct"
elif request.POST.get('UserForgotPassword') == 'Forgot Password':
form_SignUP = SignUpForm()
form_SignUP_Login = LoginForm()
else:
form_SignUP = SignUpForm()
form_SignUP_Login = LoginForm()
return render(request, 'User/SignUp.html', {'form' : form_SignUP, "error":message,'form_login' : form_SignUP_Login, "error_login":Login_message})