I am very new to Python and Django.
I am using Django - 1.4.5. I am trying to get user input from form, redirect the url with the user input like
Httpresponseredirect('abc/xyz/%s' %variable)
I could see in browser that the response is redirected, but it throws a 404 error. The urls.py also has the url defined.
urls.py
urlpatterns = pattern('',
url (r'abc/xyz/(?P<variable>)/pqr' , 'view_name')
)
Can you please throw some light on it.
On further debugging I could figure out that the issue is with passing the argument to reverse function. When I pass a static page with no arguments, it redirects to the correct view. But when I redirect with arguments it throws me a NoreverseMatch error. I am redirecting it this way
return HttpResponseRedirect(reverse('view_name', kwargs= {'group':'group'}))
In urls.py it is defined as:
url (r'app/$' , 'app.view.app') ,
url (r'^my/first/(?P<group>)/$ , 'app.webapi.json.list_record', name ='view_name;),
list_record is a function in app.webapi.json.py.
Please let me know if any more details is required.
User reverse function in you view, first add a url name:
Httpresponseredirect(reverse('my_view_name', args=[variable]))
and (like #sushail said) fix the regexp:
urlpatterns = pattern('',
url (r'^abc/xyz/(?P<variable>)/pqr/$' , 'view_name', name='my_view_name')
)
you need to use complete path as the regexpression matchs abc/xyz/..../pqr
Httpresponseredirect('/abc/xyz/%s/pqr/' %variable)
and like (like #lalo said)
urlpatterns = pattern('',
url (r'^abc/xyz/(?P<variable>)/pqr/$' , 'view_name')
)
please checkwith / at begining and end, what is the view_name, it looks like you must have such a view in project directory,or you have to add the apppath in urlpatterns
`urlpatterns = pattern('<yourappname>.views',`
Related
My views.py runs code fine when I press a button on my HTML page, views.py:
def start_or_end_fast(request):
#If starting fast, add a row to the db:
#fast_finished = False
#start_date_time using = current time
#end_date_time using = current time
if request.method == 'POST' and 'start_fast' in request.POST:
add_fast = logTimes(fast_finished=False,start_date_time=datetime.now(),end_date_time=datetime.now())
add_fast.save()
print(add_fast.start_date_time,add_fast.end_date_time)
print('Fast started')
#return render(request,'startandstoptimes/index.html')
return HttpResponseRedirect('startandstoptimes/index.html')
You can see my commented return line, this works but when I refresh the page I can resubmit the data, I want to avoid this. In researching my solution, I saw this could be solved using HttpResponseRedirect but I am not able to get this to work with my code, the more I change the more broken things become.
My application urls.py:
from turtle import home
from django.urls import path,include
from . import views
urlpatterns = [
path('', views.start_or_end_fast,name="start_or_end_fast")
]
My project urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('startandstoptimes.urls'))
]
I believe it is related to the URLs, due to the 404 message I see:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/startandstoptimes/index.html
Using the URLconf defined in myfastingsite.urls, Django tried these URL patterns, in this order:
admin/
[name='start_or_end_fast']
The current path, startandstoptimes/index.html, didn’t match any of these.
Am I going down the right route trying to use HttpResponseRedirect or is there a better solution?
class HttpResponseRedirect¶
The first argument to the constructor is required – the path to redirect to. This can be a fully qualified URL (e.g.
'https://www.yahoo.com/search/'), an absolute path with no domain
(e.g. '/search/'), or even a relative path (e.g. 'search/'). In that
last case, the client browser will reconstruct the full URL itself
according to the current path. See HttpResponse for other optional
constructor arguments. Note that this returns an HTTP status code 302.
See this link for more details: docs
As what the documentation says, HttpResponseRedirect accepts URL and not the path of your template. You should be doing it something like this:
from django.urls import reverse
return HttpResponseRedirect(reverse('start_or_end_fast'))
I want to have my custom "/login" page. So in settings.py I did a simple LOGIN_URL = '/login'. But before doing it, I want to develop all other more complex pages. I found a simple but very effective hack like this:
urlpatterns = [
# blabla
url(r'^admin/', include(admin.site.urls)),
url(r'^login/$', RedirectView.as_view(
url=reverse_lazy('admin:login'))),
# blabla
]
This means: when the user is not connected he/she is redirected to /login. In the urls, /login is converted to 'admin:login' which is admin/login. This is a "double" redirect. Everthing works fine except this:
origin URL: "/my_jobs"
redirected to "login?next=/my_jobs"
redirected to "/admin/login"
So my problem is that I want do pass again the "next" parameter in the RedirectView. I found a lot about redirection and custom login, but not something about that on stackoverflow (this is not a duplicate).
You can set query_string to True, so that query strings are appended to the URL.
RedirectView(
url=reverse_lazy('admin:login'),
query_string=True,
# You might want to set permanent=False,
# as it defaults to True for Django < 1.9
permanent=False,
)
Note that Django comes with a built in login view. You can enable it by adding the URL pattern and a simple template, which isn't much more work than your code above.
I have a URL that needs to account for the following patterns:
localhost:8000/staffing-agencies
localhost:8000/staffing-agencies/90210 (zip code)
localhost:8000/staffing-agencies/portland-or (city-state)
When I type in any of these urls in my browser, they all work as expected. However, I'm getting a NoReverseMatch error when I try to refer to this URL from Django's url template tag.
Here are my relevant url.py files:
# From urls.py
urlpatterns = patterns('',
url(r'^', include('bos.apps.search.urls', namespace='search',
app_name='search')),
)
# From search/urls.py
urlpatterns = patterns('bos.apps.search',
url(r'^staffing-agencies/'
r'((?P<city>[a-zA-Z]+)-(?P<state>[a-zA-Z]{2}))?'
r'((?P<zip>[0-9]{5}))?$',
'views.main', name='main'),
)
I thought it might be something to do with the optional parameters, but all of these variances throw the NoReverseMatch error:
Test
Test
This variance does NOT throw an error:
Test
I'm using Django 1.6.5
This not better solution, this is one of solution
url(r'^staffing-agencies/(?P<city>[a-zA-Z]+)*-(?P<state>[a-zA-Z]{2})*?(?P<zip>[0-9]{5})*?$',
'views.main', name='main'),
in views:
def main(request, city=None, state=None, zip=None):
in html:
Test
In this case url work like this,
localhost:8000/staffing-agencies
localhost:8000/staffing-agencies/-90210 (zip code)
localhost:8000/staffing-agencies/portland-or (city-state)
How can I support old and new URI versions both working without breaking the reverse()?
For example, I have:
urlpatterns = patterns('',
url(r'^(old_part|new_part)/other/$', 'some_view'),
)
In this case /old_part/other/ and /new_part/other/ point to the same view but reverse() method fails because it doesn't know how to form link properly.
Also, what if we have url(r'^(old_part|new_part)/other/', include(sub_patterns)) how can it be handled?
Do you have any ideas?
Thanks for your help.
I suppose you are migrating. This means you don't want old url work, you want it to redirect to new url. Probably with 301 HTTP code (permanent redirect).
Having several urls for same content makes your site harder to use and hurts your SEO. Permanent redirect will tell Google and any other search engine to reindex page with new address.
You can do it this way in Django:
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^new_part/other/$', 'some_view'),
url(r'^old_part/other/$',
RedirectView.as_view(url='new_part/other/', permanent=True)),
)
If you need to capture everything with a subpath, you can capture url ending and add it to redirect url this way:
urlpatterns = patterns('',
url(r'^new_part/other/$', include(sub_patterns)),
url(r'^old_part/other/(?P<rest>.*)$',
RedirectView.as_view(url='new_part/other/%(rest)s', permanent=True)),
)
You can use redirect_to generic view in Django 1.4 and earlier.
If you want without a redirect, you could try this
url(r'^(?P<url>old_part|new_part)/other/$', 'some_view', name='some_view'),
Then your view will look like this
def some_view(request, url):
...
Then call reverse like this:
# will return /old_part/other/
reverse('some_view', kwargs={'url': 'old_part'})
# will return /new_part/other/
reverse('some_view', kwargs={'url': 'new_part'})
Just redirect the old urls to the new ones (with a 301 Moved Permanently).
NB : if you really insist on supporting both sets of urls (not a good idea IMHO), you'll have to have two distinct url patterns, and choose which one reverse() should resolve to.
I have a couple problems understanding how redirect or rather reverse really work.
In the main urls.py I have:
from django.conf.urls import patterns, include, url
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
url(r'^$', redirect_to, {'url': '/monitor/'}),
url(r'^monitor/', include('monitor.urls')),
)
and in monitors.urls I have:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('monitor.views',
(r'^$', 'index'),
(r'^abc/(?P<id>.*$)', 'abc'),
)
When you call /monitor I want to redirect it to /monitor/abc so I did:
def index(request):
return redirect("abc")
def abc(render, id=None):
return render_to_response("monitor/list.htmld", {})
But I got an NoReverseMatch exception. But when I do:
def index(request):
return redirect("abc/")
then it suddenly works.
I cannot fully understand why. Why did reverse fail with abc but not with abc/? And how does reverse know that the redirect should include monitor/ as well? What if I had in the main urls.py another app called xyz which also has a abc view?
Why did reverse fail with 'abc' but not with 'abc/'?
Because it interpreted it as a view name (and you indeed have a view named 'abc', see your monitor.urls file). This means Django will call reverse to compute the URL. The value abc/ is interpreted as an actual URL which means Django won't call reverse to determine the URL.
This also explains why reverse failed: the view with name abc also requires an argument called id. Otherwise Django won't be able to lookup the URL as there is no view called abc without parameters.
Based on the documentation you should be able to reverse the URL using:
redirect("abc", id=...)
where ... is the value of the id parameter.
And how does reverse know that the redirect should include monitor/ as well?
That is because it knows what URLs are available and 1) it knows where the view called abc is defined and 2) it knows that monitors.urls is included with monitor/ in front.
What if I had in the main urls.py another app called "xyz" which also has a "abc" view?
In that case you have to use namespaces.