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

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.

Related

Django url template tag refers to url with trailing question mark

This is a question concerning a trailing question mark that gets appended to the end of the URL rendered by Django's url template tag. My goal is to have this trailing question mark removed, yet I do not know how.
On my site's main page, I have a button - a form HTML tag, really - that directs me to another link of the website.
The form looks like:
<form action={% url 'my_index' %}>
<input type="submit" value="some value" />
</form>
When clicking on that button, I am directed to the URL corresponding to the my_index view. The problem, however, is that at the end of that URL, there is a "/?" that is appended - that is, the URL is in the form "http://127.0.0.1:8000/my_app/?". I want to remove this /? at the end and also want to understand why this trailing question mark got appended.
My urls.py pointed to by the ROOT_URLCONF IS
urlpatterns = [
path('', include('my_app.urls')),
]
And my urls.py within my_app app looks like:
urlpatterns = [
path('', views.index, name='my_index'),
]
Any advice is appreciated.
you are making an GET request (default for a form) with just a submit button but without an input value. After the ? you would normally see the input values that you send with the GET request like
http://127.0.0.1:8000/my_app/?my_value=12345
As your form is empty there is no parameter appended.
You could use a
<a href='{% url 'my_index' %}'>
if you just want to call a page without any parameter
You are sending an empty form. The default method of your form is GET. So the “?” is necessary to send the form...
Why don’t you use a normal link instead an empty form?

Django - Going back to a directory at a upper level from the template with href in django

trying to keep the question simple!
I am currently in the directory which has a template cart.html:
8000/shop/cart/6/M/user
and I want to go to the directory:
8000/shop/shirts
So I just wrote in the cart.html:
<a href="{% url 'shop' 'shirts' %}">
Which gives me an error:
Reverse for 'shop' not found. 'shop' is not a valid view function or pattern name.
So, I believe that its because I am trying to go from a lower directory back into a higher directory in the hierarchy so is there a way I can edit the href in cart.html in order to go to the shirts directory?
Thanks in advance,
When you define the urls. you can chose a name to use whenever into a django template, to reffer to this url
path('shirts/',views.shirts,name='shirts'),
now, inside a template
Shirts
The name after the url within the '' is the NAME you define into your urls file
you can go to parent directory by .. so <a href='../../../shirts'> would be what you want,
or without changing the directory <a href='/shop/shirts'>.
but it's better to use {% url 'url-name' %}

url name accessing another view

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

Accessing a named view from a template in django

Suppose I have two Apps AppA and AppB
|_AppB
| |_urls.py # url(r'^create/', views.user_profile, name="name_user_profile"),
|
|_AppA
|_urls.py
|_templates
|_file.html
Now in file.html I have something like this
{% block content %}
Success !!!
goto <a href={% url `name_user_profile` % }> Manage Profile </a>
{% endblock %}
However name_user_profile cant be recognised ? Is there anything i have to do to get it recognised ?
Update:
This is what I have done so far
In my main app urls.py (the one with settings.py). I added the following
url(r'^profile/', include("UserProfile.urls" , namespace="UserProfile" , app_name="UserProfile")),
Now in my UserProfile app where the view is located I have this
urlpatterns = [
url(r'^$', views.user_profile, name="name_user_profile"),
]
and the template which which needs to call that view is located in the 3rd app
called accounts which has this
goto <a href={% url 'UserProfile:name_user_profile' % }> Manage Profile </a>
But that does not seem to work
Since your apps reference different urls.py files, in order to use named URLs you will need to access each one with its own namespace.
Check out the example in the docs here. There is a urls.py file that uses include('polls.urls'). Then, in the actual Polls app, there are a couple of named URLs, index and detail. Since they're inside an app those urls can be accessed by using their namespace - {% url 'polls:index' %} and {% urls 'polls:detail' %} respectively.
If you are working before Django 1.9, you have to specifically supply the namespace argument after the include statement. Starting in 1.9 Django automatically uses the app name for the namespace when you include a urls.py file from an app.

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.