Django form redirect - django

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

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 passing a variable through the path, causing path repetitions. how to get out of that loop?

I know this is an armature question but here goes.
I have a url path as follows:
path('projects/<s>', PL.projects),
And I pass a string from the html template by putting it into an href tag like so projects/some_string. this works once but then the base url changes to <ip>/projects/some_string. so when I try to excite the path to pass a string in that domain then I get an error as the url is now <ip>/projects/projects/some_string.
How do I set it up so that I can pass as many strings as possible as many times as possible without having to clean my url in the browser everytime.
Django has inbuilt url lookup features
path("some_random_url_link_1/", views.Link1View.as_view(), name="url_link_1"),
path("some_random_url_link_2/<int:some_id>/<slug:some_slug>/", views.Link2View.as_view(), name="url_link_2"),
in your template you can use it like this, and pass variables/parameters like this. FYI you don't need to use {{variable}} tag here
<a href="{% url 'url_link_1' %}" >Link 1</a>
<a href="{% url 'url_link_2' some_id=id1 some_slug=random_slug %}" >Link 2</a>
Learn how to use the reverse() function and the url template tag and your problems will be gone.
Those functions are built-in with Django and can handle all of that nasty URL stuff.
Reverse: https://docs.djangoproject.com/en/3.2/ref/urlresolvers/
Url Template tag: https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#url

How to pass a template tag from one template to another in django 2

I am new to Django, and template tags and HTML and have a template where I use a for loop to fill out bootstrap cards from a database. In the model I has a field Resume_link that has a PDF file. All I want to is have the PDF file displayed in a different template file and not in the card where it is too small to read. (Since I am in the loop when someone clicks the link, I just want the specific resume connected to that card to be shown in the new template.) So all I think I should need to do is somehow either pass the the index of the loop, or another variable that identifies the correct database entry. But I think I am missing something fundamental and don't understand how to pass the value of a template tag in one template another template. Is there some way to pass a variable along with the url to a view so the variable can be used to make a new template tag in the desired template?
{% for key in myres %}
...fill out other parts of cards and create the below link...
<a href="{% url "show_pdf" %}" style="font-size: 20px">
{% endfor %}
where show_pdf is the view where I want to show the whole PDF file.
and that template show_pdf is
What I would like to do is be able to pass the key.Resume_link.url, or if not that the pk for that database table to the show_pdf template.
The view for show_pdf is
def show_pdf(request):
template = 'show_pdf.html'
myres=Research.objects.all()
context = {'myres': myres}
return render(request,'mainapp/show_pdf.html', context)
you can pass the current pdf id in the url and access it.
def show_pdf(request, pdf_id):
template = 'show_pdf.html'
myres=Research.objects.get(id=pdf_id)
context = {'myres': myres}
return render(request,'mainapp/show_pdf.html', context)
In you urls.py you must write like this
path('show_pdf/?P<int:pdf_id>/', views.show_pdf, name="show_pdf")
In HTML write <a href="{% url "show_pdf" key.id %}">
Instead of:
<a href="{% url "show_pdf" %}">
... you would include a parameter, e.g.:
<a href="{% url "show_pdf" pdf_id %}">
The resulting URL would now be formatted as indicated by the corresponding urlconf entry, for instance:
http://my.web.site/resumes/show_pdf/1343
(if pdf_id = 1343 ...)
You must specify as many parameters as the corresponding urlconf entry expects, and you may use either positional or keyword syntax (but not both).
Then, when the user clicks on the link, the View specified in that urlconf will get control, and it will have the specified parameter value (1343 ...) as one of its arguments. You'd select the PDF and send it to the template to be properly presented to the user.

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 : model instance url is appending to the current url

i have some link in a template to which i want to point to particular url.
template is accessed at url : loacalhost:8000/account/profile
{% for poll in voted_poll_list %}
<h4>{{ poll.title }} </h4>
{% endfor %}
in models.py i have created the url for the poll objects to be used in a template.
def link_url(self):
return "polls/"+ "allcat/" +str(self.id)
the problem is when a link in template is clicked it is point to the loacalhost:8000/account/profile/polls/allcat/1 instead of loacalhost:8000/polls/allcat/1 which matches to url pattern
url(r'^polls/(\w+)/(?P<pid>[-\d]+)/', 'pollsite.views.poll_detail', name='detail_poll'),
the problem is link url of object is appended to current url. how can i avoid this ?
#garnertb 's solution works, but you should probably look into using the reverse function rather than hardcoding your url.
Something like:
return reverse('detail_poll', self.id)
This will not only take care of the leading slash, but also avoid trouble if you ever start changing your url configuration.
Try leading the url with a forward slash:
def link_url(self):
return "/polls/allcat/" +str(self.id)