URL and regex with django - 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']

Related

django url concatenation. I don't want to concatenate

when I do something in 'profile' page,
the url is concatenated to the next of 'profile'.
but I want to link to just 'signout'. not 'profile/signout'
this is my urls.py.
when ever I do something in 'profile'page,
the href link is concatenated to 'profile'url.
this is href source.
since this href source is header.html,
this page is included another pages.
and in the other pages, it works well.
only in profile page, the href url is concatenated to 'profile/1' url.
how can I fix it?
Yes, a URL not starting with a slash or a scheme is a relative URL. href="foo" is equivalent to href="./foo", i.e. it refers to the path foo relative to the current path. If you want the top-level path, you want href="/foo".
In Django you're supposed to use the {% url %} template tag to generate URLs, you don't hardcode them. Django will take care to generate the correct URL; especially if you move the app around to other environments, the URL may require a prefix or such, so you should never hardcode the URL.

Url mismatch, django template link

I've got a prob with a link in the sidebar of my django site, in the template it's like that:
<li>Profile</li>
while in the urls.py:
url(r'^(?P<user_id>\d+)/profile/$', 'auth.views.show_profile', name='profile')
When i access it from the main page with url: e.g /1001/profile/ it loads fine but when I try to access it from another subpage with url: e.g /1001/forms/profile/ i get the error: The current URL, /1001/forms/profile/, didn't match any of these. How can i fix this?
It is because "profile" is a relative URL, and a relative URL is appended to the current URL - the resulting address is not valid across the whole site. Seems like you should use an absolute URL in your case.
At the template you can try something like:
Profile
UPDATE
To get request available in templates you have to add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS. I'm not sure if it is added by default.
You must have to add your second subpage url in urls like you did for /1001/profile/
url(r'^(?P<user_id>\d+)/form/profile/$', 'auth.views.show_profile', name='profile_form')
and also correct your code as #Paulo mentioned or you can also do it through reverse url.
Profile

Getting the root url in Django

In my view, I want to make a request to mine.com/more/stuff/ from an arbitrary page such as mine.com/lots/of/stuff/to/use or from mine.com. Thus, I can't make this a relative request using ./ or ./../ type things. Do I have to use a context processor to do {{URL_BASE}}more/stuff/? Is there a set way to do this in Django or a best way?
why don't you use named urls? it's always works.
for example {% url 'admin:index' %} always printed as url to admin(in case if you using default django.contrib.admin app).
if you'll have in urls.py smth like
url(r'^lots/', Lots.as_view(), name='lots'),
then just use smth like
{% url 'lots' %}
Don't hardcode your urls!
Instead of a relative url, use an absolute url: /
If you're on mine.com/lots/of/stuff/to/use or mine.com, hitting a link with url: /foo/ will both go to mine.com/foo/

django url tag problem

I have problem with url tag. I want to redirect to a function that is in for eg
project_name.forum.views.function. Here is how i try to create url
{% url forum.views.function %}
it gives me this error:
Caught ViewDoesNotExist while rendering: Tried forum in module project_name.forum.views. Error was: 'module' object has no attribute 'forum'
I added this url in urls.py(I can access it directly) What am I doing wrong?
The url tag is used to reference named urls. E.g.
url(r'^$',
login_required(views.user_babies),
name='babystats_user_babies'),
Then you use {% url babystats_user_babies %} (the url pattern name, not the view name)
It sounds more like an incorrectly set up URL conf. You get that error when you specify a view that doesn't exist.
The url tag failing gives you a failure to reverse url with params... message.
What does your URL conf look like? Does project_name.forum.views.forum exist?
I mean, I find it odd you can visit the page at all, but that's the first place I'd look.
I have seen this error before with django url reversing stemming from the urlconf being set up with a root like projectname.app.views.view instead of app.views.view, so it chokes on the reverse without the name of the project.
Another common issue would be that the url takes an extra parameter that can be blank, and it needs you to pass an empty string or whatnot.

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