Can any one help me to learn reverse url in django - django

I have a menu in my project and I want to call different Views.
For example, if I have a menu like:
home | about Us | Contact-Us
If I click on home it will call home view successful and then when I click again on home it takes to home/home or when I click on about-us it will take me to home/about-us
Every call (click) on menu will increase home/about-us/home........ like this.
How can I correct 'Url' patterns? I read the documentation but I did not get any idea or solution.

Assuming you have a URL defined for the About page that looks like this:
url(r'^about/$', 'website.views.about', name='about'),
You can reverse a URL in a template like this
{% url 'about' %}
Or you can reverse a URL in your views.py like this
return redirect(reverse('about'))

Related

URL and regex with django

I have a problem with the urls.py in my django project
I have an url like this:
http://127.0.0.1:8000/cars/?page=2
then when i clik on next it will be :
http://127.0.0.1:8000/cars/cars?page=3
and I have en error :(
the url is :
url(r'^cars/$page=n', TaskViewSet.as_view()),
This is happening because you are linking next/previous pages like <a href="cars?page={{ page_obj.next_page_number }}"> in your template.
You should make following changes to your project:
Rename url conf, like #jonatron suggested:
url(r'^cars/$', TaskViewSet.as_view()),
Then in your templates, make sure you link next/previous pages with their absolute positions, like this:
next
Also, you can name your url conf and generate the full url without having to rewrite everytime:
url(r'^cars/$', TaskViewSet.as_view(), name='cars'),
next
You need:
url(r'^cars/$', TaskViewSet.as_view()),
Anything after the ? is a query string, which does not get matched in the URL patterns. You can access the ?page value in the view, eg:
page = request.GET['page']

Django advanced nesting urls

Welcome,
I've got a problem where I`m trying to make a deep nesting.
The thing is that I have Menu that has SUBMENU that can have multiple categories and those categories can have multiple services available and those services can have multiple products.
Writing urls for that specific thing was easy but when I'm trying to create a product that has no category and I want it to be displayed directly in SUBMENU the problem is with writing url for that specific product that doesnt overlap with SUBMENU slug.
For example:
MENU > SUBMENU1 > CATEGORY 1 > AVAILABLE SERVICES > PRODUCT 1
MENU > SUBMENU2
MENU > SUBMENU3 > PRODUCT 2
all of those names are slugged and my urls.py looks like this:
url(r'^uslugi/(?P<category_slug>\S+)/(?P<services_slug>\S+)/(?P<service>\S+)/$', views.show_service_details, name='show_service_details'),
url(r'^uslugi/(?P<category_slug>\S+)/(?P<services_slug>\S+)/$', views.show_services, name='show_services'),
url(r'^uslugi/(?P<category_slug>\S+)/$', views.show_categories, name='show_categories'),
url(r'^uslugi/(?P<product_slug>\S+)/$', views.show_product_without_cat, name='show_product_without_cat'),
The thing is that when I try to enter a product with no category (just placed in submenu), my urls are calling the show_category view. Changing sequence of those urls won't resolve my problem, because I won't be able to enter my show_categories because django will try to execute show_product_without_cat
I there a reasonable solution for this without redesigning all structure ?
Unfortunetely even saying to django in template to call specific VIEW doesn't help at all. It just goes through all urls and matches the first one
<a href="{% url 'show_product_without_cat' i.url %}" >
<img src="/media/{{ i.image }}"> <br/>
</a>
URL dispatcher finds the first url that matches the request path and calls the found view. You have the same url regex for both show_categories and show_product_without_cat views so only the first occurrence of url will work.
The only solution is to create an intermediate view which will check the slug against Category or Product model and call the appropriate view.
def product_or_category(request, slug):
if Product.objects.filter(slug=slug).exists():
return show_product_without_cat(request, slug)
return show_categories(request, slug)
And assign the url to this view:
url(r'^uslugi/(?P<slug>\S+)/$', views.product_or_category,
name='show_product_or_category'),
But note that with this solution you can't have a category and product with the same slug.

django url parse formatted url

I'm in the design stages of a single page web app, and would like to make it so that a user can click on a formatted URL and the data requests will load in the page.
For example, a url of http://www.mysite.com/?category=some_cat will trigger the Category view with the relevant data.
My intention is to parse the URL, gather the data, then pass it to the index.html template for rendering on page load. Once the page has been loaded, a Javascript trigger setting will trigger the appropriate button to load the client view.
However, I'm having an issue setting up the URL parser, as the following settings are not matching the example url above.
from app.views import app_views, photo_views, user_views, admin_views
urlpatterns = patterns("",
url(r'^/(?P<category>\d+)/$', app_views.index)
)
You're confusing between sending information through your urls with GET and formatting you urls with arguments for the view functions. Say I am visiting a site called http://www.mysite.com/ and the page has a form that looks like this:
<form>
<input type='text' name='category' id='category'></input>
<button type='submit'>Send!</button>
</form>
upon clicking, the url will automatically change to http://www.mysite.com/?category=<value of input>. The ? marks that everything afterwards should be treated as GET data, with the syntax of <id>=<value>. You can then access them like so:
def response(request):
category = request.GET['category']
formatting urls is different, because it means looking for patterns that are part of the url. i.e. a pattern that looks like r'^/(?P<category>\d+)/$' will look for this: http://www.mysite.com/<category>/ and it will send it to the request in your views as an additional argument like so:
def response(request, category):
...
The regex is used to define how you recognize that part of the url. For example, the \d+ you're using means that category needs to be a number. You can search how to define different types of patterns according to your needs
Note that with GET you are sending the data to the same view function that rendered the page you are currently visiting, while using a different url means you tell it where to go through your urls.py (usually a different function). Does that make things a bit clearer?

Django: How do I pass request URLs through urls.py?

I have a django app with the following entries related to logging out in urls.py:
(r'^logout/$', 'django.contrib.auth.views.logout', {'next_page':'/logout_page/'}),
(r'^logout_page/$', 'django.views.generic.simple.direct_to_template', {'template':'registration/logged_out.html'}),
I want to add a "Log back in" link in logged_out.html, which requires me to know the location (URL) from where the user originally logged out.
How do I reference that URL in the logout dispatcher entry, and how do I pass it to the logout_page entry?
Thanks in advance
I do not exactly understand what you mean by "passing variables here" but you can pass variables to the views from urls like this
r'^(category/page/(?P[0-9]+)/$
page_number is passed as a variable to the url
But what I think you are looking for is to pass the previous prefer which you should be able access in the template like this :
{{ request.META.HTTP_REFERER }}
So you can do something like :
Login
in the template of logout_page to achieve what you are looking for

tag url template django doesn't work

I trying to implement the url template tag into my project.
I have a button that allows the user to save the data he is seeing.
So the url of this button is this:
(2)url(r'^nameviews/download/$', 'my.path.is.this.to.download' name="load"),
template:
Download
the url of the page that shows the information, and where the button is located is:
(1)(r'^nameviews/$', path.to.page),
but when I tried to click on the button (it should appear the url 2)it doesn't open the file with the data but instead gives me the same url that the main page (1)
the html validator gives me a error on the
<a href="">
It seems it doesn't recognize the url tag.
Anyone has any idea?
Resolved! I didn't really understood what's happen because I didn't change much but should have been some silly mistake.
Sorry!
Thanks any way :)
EDIT
Be careful with the order of the urls. At urls.py try this order:
url(r'^nameviews/download/$', name_of_view, name="load"),
url(r'^nameviews/$', name_of_view, name="first page"),
name_of_view is equal if the view is the same