what is "request" in Django view - python

In the Django tutorial for the first app in Django we have
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
And then the urls.py has
from django.conf.urls import url
from polls import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Now my question is what is the "request" parameter passed to the index function, also when the function index is called in urls.py it is not passed and variables it is just called as views.index in the line url(r'^$', views.index, name='index'),

The request parameter is a HttpRequest object, which contains data about the request (see the docs for django 3.2).
In your urls file, you are not calling the view.index function, just listing a reference to it. Django then calls the function when a matching request comes in and passes the HttpRequest object as a parameter.

This doesn't directly answer your question, but I suggest you watch this video:
A Scenic Drive through the Django Request-Response Cycle
This is a PyCon talk Dan Langer gave this year and showed how request and response work under the hood.

From Django Docs. Request came from User that want to load page.
When a page is requested, Django creates an HttpRequest object that
contains metadata about the request. Then Django loads the appropriate
view, passing the HttpRequest as the first argument to the view
function. Each view is responsible for returning an HttpResponse
object.

Related

Avoid Django form being resubmitted by using HttpResponseRedirect

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

django redirect inside of urls.py

I plan on having several different apps inside my django project, each for a new technology I want to play around with. As I work on each on, I want to have the root URL path redirect to the project I'm working on.
Directory Structure:
backyard/
my_project/
views.py
backyard/
urls.py
backyard/backyard/urls.py:
from django.conf.urls import patterns, include, url
from django.shortcuts import redirect
urlpatterns = patterns('',
url(r'^$', redirect('my_app/')),
url(r'^my_project/$', 'my_project.views.homepage'),
)
backyard/my_project/views.py
def homepage(request):
return render_to_response('my_project/index.html', {'data':data})
When I access the page at http:machine-name:8000/ I get an error saying The included urlconf backyard.urls doesn't have any patterns in it I most definitely have URLs in my urls.py, what is the issue?
A URLconf needs to map regexes to views -- the redirect function doesn't return a view; rather, it returns an HttpResponse. You can look at this question for an example of how to define a redirect directly in the URLconf.

How to Implement 301 Redirects with Django/Heroku

I'm looking to redirect a list of old URLs to a list of new URLs in a Django/Heroku app.
Since I'm using Heroku, I can't just use an .htaccess file.
I see that rails has rack-rewrite, but I haven't seen anything like that for Django.
Django has redirects app, which allows to store redirects list in database:
https://docs.djangoproject.com/en/dev/ref/contrib/redirects/
Also here a generic RedirectView:
https://docs.djangoproject.com/en/1.3/ref/class-based-views/#redirectview
And the lowest level is HttpResponseRedirect:
https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponseRedirect
You can use redirect. Please check below code.
from django.shortcuts import redirect
return redirect(
'/', permanent=True
)
It worked for me.
Try redirect_to
Example from the docs for a 301 redirect:
urlpatterns = patterns('django.views.generic.simple',
('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/'}),
)
While the redirects app mentioned in the accepted answer is a pretty nice solution, it also involves a database call for every 404 error. I wanted to avoid this so ended up just manually implementing this in a URL conf.
"""redirects.py that gets included by urls.py"""
from django.urls import path, reverse_lazy
from django.views.generic.base import RedirectView
def redirect_view(slug):
"""
Helper view function specifically for the redirects below since they take
a kwarg slug as an argument.
"""
return RedirectView.as_view(
url=reverse_lazy('app_name:pattern_name', kwargs={'slug': slug}),
permanent=True)
urlpatterns = [
path('example-redirect/', redirect_view('new-url')),
]

Understanding django.shortcuts.redirect

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.

Django urls straight to html template

Learning django & python.
Just set up a new site after doing the tutorial. Now for arguments sake say I want to add a bunch of About us, FAQ basic html pages with very limited dynamic elements do you go ahead and write a new line in my urls.py file for each page? or is their some neat way to say map all * *.html to the relevant .html file directly?
In general even if it does require a view will I have to write a new line in the url.py file for every page?
As long as there is some uniquely identifying section in the URL, you will not need to create an entry in urls.py for each direct-template url.
For example, you could say that all urls ending in ".html" are referencing a direct file from the templates.
urlpatterns = patterns('django.views.generic.simple',
(r'(.+\.html)$', 'direct_to_template'),
# ...
)
Take a look at http://docs.djangoproject.com/en/1.2/ref/generic-views/#django-views-generic-simple-direct-to-template for details.
Currently the best way to do this is using TemplateView from generic class-based views:
from django.views.generic.base import TemplateView
url(r'^$', TemplateView.as_view(template_name='index.html'), name='home'),
Write a url which grabs the static pages you're interested in
url(r'^(?P<page_name>about|faq|press|whatever)/$', 'myapp.staticpage', name='static-pages')
The staticpage view function in myapp
from django.views.generic.simple import direct_to_template
from django.http import Http404
def staticpage(request, page_name):
# Use some exception handling, just to be safe
try:
return direct_to_template(request, '%s.html' % (page_name, ))
except TemplateDoesNotExist:
raise Http404
Of course, you need to follow a naming convention for your templates, but this pattern can be expanded upon as needed.
This is better than the .+\.html pattern because it will treat templates which don't exist as 404s, whereas .+\.html will blow up with 500 errors if the template doesn't exist.
If you're using the class based views because direct_to_template has been deprecated, you can create a simple wrapper that renders your own templates directly:
from django.views.generic import TemplateView
from django.template import TemplateDoesNotExist
from django.http import Http404
class StaticView(TemplateView):
def get(self, request, page, *args, **kwargs):
self.template_name = page
response = super(StaticView, self).get(request, *args, **kwargs)
try:
return response.render()
except TemplateDoesNotExist:
raise Http404()
and in your router:
from myapp.static.views import StaticView
urlpatterns = patterns('',
url(r'^(?P<page>.+\.html)$', StaticView.as_view()),
# ...
)
One way to do this would be to write a single custom view that wraps the direct_to_template generic view. The wrapper could accept a parameter and accordingly form the name of the template and pass it to direct_to_template. This way you can route multiple pages with a single URL configuration.
Something like this:
url(r'^foo/(?P<page_name>\w+).html$', 'my_static_wrapper', name = 'my_static_wrapper'),
def my_static_wrapper(request, page_name):
# form template name and call direct_to_template
That said I suspect that there are better solutions out there though.
Slight Changes for latest versions of Django.
from django.views.generic.base import TemplateView
urlpatterns = [
path('',TemplateView.as_view(template_name='index.html'), name='home'),
]

Categories