I am trying to make my first ever Django app. I have made a simple form where a user can enter a username
. When the user clicks on submit, I want to redirect him to the url containing that username
.
For example, Lets say my form is in the url www.mydomain.com/form
if the user enters the username = 'myname
, I want to take him to the url www.mydomain.com/myname
and display Hello myname, Welcome to www.mydomain.com
.
Any idea how can I achieve that?
here is what i am currently doing
from django.conf.urls import include, url
urlpatterns = [
url(r'^$', 'temp.views.home',name='home'),
url(r'^temp/$', 'temp.views.temp',name='temp'),
url(r'^entry/(?P<entry>[A-Za-z0-9_.\-~]+)','temp.views.mimic',name='mimic'),
]
from django.shortcuts import render
from django.http import HttpResponseRedirect,HttpResponse
def home(request):
return render(request,'home.html',{})
def temp(request):
entry=request.POST['entry']
print entry
return HttpResponseRedirect("/entry/"+entry)
def mimic(request,entry):
return HttpResponse(entry)
<form method="POST" action="/temp/">{% csrf_token %}
<INPUT name='entry'>
<input type='submit' text='done'>
</form>
Now my question is can i somehow avoid this temp part in both urls and views n do the whole redirection in one step?
P.S. i know that i need to define a few checks on the form. I will add them later. currently lets assume that the entry made by the user matches with the regex defined in the url
答案 0 :(得分:1)
What you need:
name = forms.CharField(...)
clean_name(self)
method raising ValidationError
for unknown profilesdjango.shortcuts.redirect
with your profile view and the name.答案 1 :(得分:1)
您需要确保以下内容:
urlpattern
以便请求
/<username>/
指向您为个人资料模型设置的详细信息视图如果您对这些步骤感到困惑,我强烈认为您找到了一个吸引您并完全遵循它的Django教程。它将指导您执行上述所有步骤。