url name accessing another view - django

I have two path in urls.py file. When I hit a url,instead of picking intended url it is picking another one why?
Can anyone please help me.
This is my urls.py file:
from . import views
from django.urls import path
app_name = 'olx'
path('<slug:category_slug>/',views.product_list, name='product_list_by_category'),
path('myPost/', views.myPost, name='my_all_post'),
In my html file I am using anchor tag like this:
<li class="nav-item">
<a class="nav-link" href="{% url "olx:my_all_post" %}">My Post
</a>
</li>
I tried replacing double inverted commas with single inverted commas like this:
href="{% url 'olx:my_all_post' %}"
but still it is picking another path:
<slug:category_slug>/
but if I remove the below path from my urls.py file then it is picking the correct one.
path('<slug:category_slug>/',views.product_list, name='product_list_by_category'),

The URL pattern,
path('<slug:category_slug>/',views.product_list, name='product_list_by_category'),
will match any url such as,
/something/
or
/something-else/
or
/myPost/
what you could do is change that pattern to something like,
path('categories/<slug:category_slug>/',views.product_list, name='product_list_by_category'),

The problem is the ordering of your URL pattern definitions.
'myPost' is a valid slug, so '<slug:category_slug>/' will match it.
Change your order like so:
path('myPost/', views.myPost, name='my_all_post'),
path('<slug:category_slug>/',views.product_list, name='product_list_by_category'),

Related

How to create correct url in tepmplate Django

I have this link in my templates:
<a href="{% url 'main:user-main-account' %}?=client_account{{ client_account.id }}">
like a link it looks like:
https://mysite.ua/main/user_main_account/?=client_account=1
How can I change my template link to get link like this:
https://mysite.ua/main/client_account/1/user_main_accounts
My 2 urls in urls.py look like this:
path('user_main_account/<int:pk>/', ........)
path('client_account'/<int:pk>/'.........)
Please help me!I am stuck of 3 days.
You can add the data into the url tag it will be like
{% url 'main:user-main-account' client_account.id %}
this will generate the URL you looking for also Read the Docs

django 1.11 html "href" doesn't work with template url

My urls.py
urlpatterns = [
url(r'^index', views.index, name='index'),
my views.py
def index(request):
return render(request, 'index.html', {})
index.html
<ul>
<li>All Contacts</li>
</ul>
My page with the href hyperlink not working
The source:
So I had a look at https://www.w3schools.com/tags/att_a_href.asp and it indicates that relative paths only work if it's pointing to a file. Not sure what I'm missing here?
<ul>
<li>All Contacts</li>
</ul>
use this as href needs to be before the <li> tag
Here:
All Contacts
Your <a> tag is empty. You want to put the link text inside the tag:
All Contacts
Oh and while we're at it:
I had a look at https://www.w3schools.com/tags/att_a_href.asp and it indicates that relative paths only work if it's pointing to a file
The exact text is: "A relative URL - points to a file within a web site (like href="default.htm")". But that's still complete BS, there's no notion of file here, a "relative url" (actually an absolute or relative path) is resolved against the current domain (and the current path if it's a relative path), how the resulting url is served depends on the software serving this resource. FWIW, Django's urls (the one built by the {% url %} tag) are always absolute path.

Build-in views?

I installed a module to Django and this module has ceertain views within itself. Also it comes with a demo to see the features. I am looking at the code from the demo for a while now but I cant figure out how a template is rendered over the build in views.
The module i'm working with is swingtime:
Here is an example:
Excerpt from the template:
<a class="plain" href="{% url 'swingtime-daily-view' prev_day.year prev_day.month prev_day.day %}">
URL Directory
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='intro.html'), name='demo-home'),
url(r'^karate/', include('karate.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^docs/?$', RedirectView.as_view(url='/docs/index.html', permanent=True)),
url(r'^docs/(?P<path>.*)$', serve, dict(document_root=doc_root, show_indexes=False))
]
So the url must somehow link to a built-in template i suppose?
When I try to implement similar code in my project the url can not be reversed?
And also: How would one edit the template for such a build in view?
Thank you!
Check the urls.py file in the karate module. you will find the url there.
It will be something like
urlpatterns = [
...
url(r'daily-view', view='view_name' name='swingtime-daily-view'),
...
]
Now the name argument of the url is the important thing you're looking for as I don't know the actual path or view set there.
So when you have something like this
<a class="plain" href="{% url 'swingtime-daily-view' prev_day.year prev_day.month prev_day.day %}">
The url dispatcher will look through the app urls for the url with this view....this is called named url patterns.
Read more on how url patterns work URL DISPATCHER IN DJANGO

Could not parse the remainder: '/{{menu.Info.page}}' from ''item'/{{menu.Info.Page}}'

<img id="page" class="abc" src="{{STATIC_URL}}page/code_251.png" style=""/>
Hitting this url like localhost:8000/app/page works fine.
If I want something from views and append that pageid with url then showing error as Couldn't parse.
From views:{{page.pageid}}
output url should be :localhost:8000/app/?pageid=xx
for this i tried with below syntax:
<img id="page" class="abc" src="{{STATIC_URL}}page/code_251.png" style=""/>
But above syntax did't worked for me.
urls.py
url(r'^(page)(?:/(?P<page_id>[0-9]+))?/$',page_ViewDetails_TemplateView.as_view(),name="page"),
May be some changes need to be done on urls.py as well.
Can someone share some idea!!
You're confused about at least two things here.
Firstly, you would never use {{ }} inside a tag. You're already in the template language context there: you have access to variables directly.
Secondly, the {% url %} tag works on urlpattern names and parameters, not literal URLs. And your page URL does not expect a querystring value for page_id: it expects it as part of the path. Your generated URL needs to be "/page/3", not "/page?page_id=3".
So your URL tag is just:
<a href="{% url 'page' page_id=page.pageid %}">

Django url handling?

After decoupling url file to our app we are facing problem:
Example:
http://www.oursite.com/ourprefix/xyz/wsz
How to handle urls in template ( to accomodate for any prefix(ourprefix) in url)
How to do HttpResponseRedirect without hard-coded urls (also outprefix problem is present here)
Use named urls in urls.py.
Use the {% url name %} template tag. It will insert the correct path.
Use reverse('name', **kwargs) for the redirect.
an example:
in proj/urls.py:
patterns = patterns('',
(r'^prefix/', include('proj.app.urls') ),
)
in proj/app/urls.py:
patterns = patterns('',
url(r'object/^(?P<pk>\d+)/edit/', edit_object_view, name="edit"),
)
in proj/app/views.py:
return HttpResponseRedirect(reverse('app:edit', {'pk':pk}))
in proj/app/templates/app/my_template.py:
<a href="{% url app:edit pk=pk %}"> <!-- generates /prefix/object/123/edit/ -->
If I understand you right, you want to resolve a particular view to a URL inside the template?
You should use the url-reverse method in Django. See here.
1) For the template, you can use:
Link
Where the "prefix" is a variable set in your Context that you pass to the template. You can also dynamically pick the right URL:
{% url application.views.viewfunc parameter1 parameter2 %}
See here for more details.
2) So to HttpResponseRedirect, you can do:
HttpResponseRedirect(reverse(your_view_function))
It also accepts parameters.