How to create correct url in tepmplate Django - 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

Related

How to get URL without required parameters

I have the following path in my URLS:
path('remove_order/<int:order_id>', RemoveOrder.as_view(), name='md_remove_order')
What I would like to do is get the URL WITHOUT specifying an order_id in my template. Like this:
{% url 'md_remove_order' %}
so it gives me something like this:
"remove_order/"
I will then pass this value to my Javascript where the order_id will be added dynamically.
You can use re_path
re_path('remove_order(?:/(?P<pk>[0-9]+))?/$', RemoveOrder.as_view(),name='md_remove_order')
So it will both work for remove_order/ and remove_order/123/
An easy way to do that is to create another url that points to the same view but without the parameter:
urlpatterns = [
path('remove_order/<int:order_id>', RemoveOrder.as_view(), name='md_remove_order'),
path('remove_order/', RemoveOrder.as_view(), name='md_remove_order_without_params'),
]
Then use that new url
{% url 'md_remove_order_without_params' %}

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 %}">

Is it possible to display a specific block from a template in django?

Just like in html when you reference a section of a page with for instanceVisit the Useful Tips Section
Would there be a way to do a similar thing in django if for instance I wanted to load my page straight to the tips section? I am extending base.html to my home page that has a tips section. Right now i have a static url home i want to do the exact same but with djangos dynamic url something like {% url 'home'/#tips %}
You can just add the fragment identifier right after the URL returned by the {% url %} template tag:
home

Django path to current page

I have a django template page, and want a link from this page, containing current URL, for example, I am on /article/11 and want link to /article/11/remove
I tried the following construction:
Remove article
But I get link to /article/remove instead of /article/11/remove
However when I change it to
<a href="{{ request.path }}">
I get link to /article/11
How can I get URL not trimmed?
I don't see why it doesn't point you to /article/11remove, which is what it sounds like it should do, but either way, you're missing a slash. Try <a href="{{ request.path }}/remove"> instead.
However, that's really not the right way to do it. You shouldbe using {% url 'name_of_remove_view' %} to get the url, not assuming it's going to be wherever you are plus /remove.
Edit: In that case, your problem is probably that {{ request.path }} is not outputting anything at all. That would explain why just having "remove" would take you to /article/remove, and having "" would take you to where you currently are, due to the way that relative URLs work. You might want to make sure that you have a request object at all in your template environment.

Django form redirect

I have a form that and after the user fills in this form, I want to redirect to a URL that looks something like this
r'^user/(?P<id>\d+)/$'
The <id> is the primary key in the database...how do I go about this coz I'm stuck at this point
Ok, so in your .html file, you should have an action= in your form, which sort of looks like that regular expression. At the very least it should look something similar to:
action="/user/{{ object.id }}/"
object.id would be replaced with whatever object you passed to the html file in your view.
The url template tag allows you to save some repetition here. It also prevents you from breaking the form if you change urls.py. If your urlpattern looks like this:
(r'^user/(?P<id>\d+)/$', 'user.views.detail')
Then you could use the template tag like this:
<form action="{% url user.views.detail object.id %}">