Opening external Url in Django template - python

I've a Django template like this:
<ul>
{% for url in urls %}
<li>{{ url.url_title }}</li>
{% endfor %}
</ul>
Url is a model that stores Url name and Url title of that particular url. I thought by using this template, I might be able to open the page and get redirected to the external url specified in:
<a href="{{ url.url_name }}">
Turns out, I can't. How do I achieve this? I'm a newbie in Django and don't know what to do.

I supposed url_name is Charfield type, and it is something like example.com or starting with http or https, for all above cases, the below worked for me:
<a href="http://{{ url.url_name }}">
If you used URL type for the url_name, then your solution should work and your problem is not with tag.

Please, try with:
<a href="{{ url.url_name }}" target="_self">

Related

How to link an ID generated URL to the <a href> tag

I am making my own portfolio website using Django.. So the idea is: Make posts containing websites that i've developed. All these posts will be displayed in a page called "Projects" and then you can access a single project to read about it. Every project i upload have an ID, so i used this code to acess each project page:
# Single project page
path('projects/<int:post_id>/', views.project, name='project'),
Then in my PROJECTS page i have a simple(for now) HTML code to show all my projects and link them to the single project page. What i want to know is how to LINK this ID generated URL in this code:
<ul>
{% for project in projects %}
<li>{{ project }}</li>
{% empty %}
<li>No project have been added yet.</li>
{% endfor %}
</ul>
I tried using:
<li><a href="{% url 'main_portfolio/projects/<int:post_id>/' %}</a></li
And some other things but nothing i try is working.
Maybe try <a href="{{ project.get_absolute_url }}">, or
<a href="projects/{{ project.id }}/">
You can work with the {% url … %} template tag [Django-doc]:
<li>some text</li>

page not found in django?

i am new to django. this is the error i keep getting when i try to visit node.name page.
page not found Request URL: http://127.0.0.1:8000/Category//
urls.py
Category/<category_id>/ [name='productlisting']
index.html
{% load mptt_tags %}
<ul class="root">
{% recursetree listing %}
<li>
{{ node.name }}
</li>
{% endrecursetree %}
</ul>
urls.py
path('Category/<category_id>/',views.productlisting, name='productlisting'),
path('Category/<category_id>/product/<product_id>/',views.details, name='details'),
thanks in advance.
First you have an extra ending / in the endpoint you are trying to hit.
This is not how we name urls in anchor tag. Please refer here on how to name url and how to use it in template
so basically idea is that you would name url in urls.py in following manner:
path('Category/<int:category_id>/',views.productlisting,name='productlisting'),
then you would use this url name in template as:
{{ node.name }}

How to add new created page url to modified django-oscar

I'm a beginner in django-oscar and I try to manage a new view on the page.
I've already created two pages with django-oscar dashboard,
https://ibb.co/cM9r0v
and made new buttons in the templates:
Lib/site-packages/oscar/templates/oscar/partials/nav_primary.html
https://gist.github.com/Kalinar/076fc8144869c3b50fc0bc9e52f825e4
I have no idea how to make a good a href="???" to new pages in buttons ... can someone help?
Maybe there is better way to do it, can you explain it to me?
Oscar uses Django's flatpages app, so you can use their template tags to dynamically add in navigation links to the pages you create.
{% load flatpages %}
{% get_flatpages as flatpages %}
{% for page in flatpages %}
<li class="dropdown active">
<a href="{{ page.url }}" class="dropdown-toggle" {% if not expand_dropdown %} data-toggle="dropdown"{% endif %}>
{% trans page.title %}
</a>
</li>
{% endfor %}
You can find more information on the flatpages app in the Django flatpage documentation.
You already know the title and of course the id of the page, and each page is an instance of the model PagePromotion, just query the model for such name/id and use the attribute page_url of the returned instance to send it within a variable in the context of the view that renders the menu.
Then in the template:
href="{{ variable_containing_url }}"

Django dynamic links

I'm new to Django so this could be a stupid question, however I'm struggling to find an answer searching Goolge. my question is as follows...
In other languages when creating links within HTML pages you have something like a buildURL(page) function that creates a dynamic link i.e.
<a herf="buildURL(pageName,queryObjects) >link</a>
This would generate the URL with parameters.
Is there a function in Django that builds URLs in the correct format i.e. generate a fully-qualified URL to a Django page with parameters?
In your templates, you never should hard write url view. Instead of this, a good practice is to named each url and reference this name in templates.
To name a url:
from django.conf.urls import patterns, url
urlpatterns = patterns('',
#...
url(r'^article/(\d+)/$', 'news.views.article', name='article_by_id'),
#...
)
To reference url in template:
<ul>
{% for article in articles %}
<li>Article {{article.name}} </li>
{% endfor %}
</ul>
Learn more in URL Distpatchar django documentation.
from what I see in the django documentation it should be
<ul>
{% for article in articles %}
<li>Article {{article.name}} </li>
{% endfor %}
</ul>
i.e. 'article_by_id' instead of article_by_id. This works for me

use slugify in template

I want to have SEO-friendly URL,my current url in urls.py :
(ur'^company/news/(?P<news_title>.*)/(?P<news_id>\d+)/$','CompanyHub.views.getNews')
I use it in template:
{% for n in news %}
<a href="{% url CompanyHub.views.getNews n.title,n.pk %}" >{{n.description}}</a>
{% endfor %}
I use news_id to get news object with that PK .
I want to convert this url:
../company/news/tile of news,with comma/11
to:
../company/news/tile-of-news-with-comma/11
by doing some thing like this in template:
{% for n in news %}
<a href="{% url CompanyHub.views.getNews slugify(n.title),n.pk %}" >{{n.description}}</a>
{% endfor %}
I checked out these questions:
question1
question2
question3 and this article but they save an slugify field in database while I wanna generate it on demand.in addition I want to run a query by news_id.
I think this question is good,but I don't know how to use news_id to fetch my news object
This will generate the needed url:
{% for n in news %}
<a href="{% url CompanyHub.views.getNews n.title|slugify n.pk %}" >{{n.description}}</a>
{% endfor %}
The examples above save slugify_field in database, as they later search for it. Otherwise in database you'll have a normal title, and slugified title in code for searching.. No easy way to compare them. But the way you've explained is simpler. You will have this kind of view:
def news(request, slug, news_id):
news = News.objects.filter(pk=news_id)
UPDATE: To use unicode symbols in slugify, you'll need a conversion first. Look at this: How to make Django slugify work properly with Unicode strings?. It uses the Unidecode library
Then add a custom filter:
from unidecode import unidecode
from django.template.defaultfilters import slugify
def slug(value):
return slugify(unidecode(value))
register.filter('slug', slug)
then in your template use this:
{% load mytags %}
<a href="{% url CompanyHub.views.getNews n.title|slug n.pk %}
Here is an example:
{{ "影師嗎 1 2 3"|slug}}
renders as:
ying-shi-ma-1-2-3
Have you tried n.title|slugify and see if that works for you.
ref: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#slugify
Note: although this is possible, just make sure the 'slugified' element is never used for any part of routing... (ie, purely for display only)

Categories