I'm developing a tourism website using flask. I have a destinations folder with names of cities. When the dropdown navigation item is clicked, these cities appear and upon click on a city, the page should be redirected to the relevant city. The navigation code is as follows;
<li>Destinations
<ul class="nav-dropdown">
<li>London</li>
<li>Paris</li>
<li>New York</li>
<li>Moscow</li>
<li>Las Vegas</li>
<li>Pataya</li>
</ul>
</li>
The demo.py code is as follows;
from flask import Flask, render_template
#app.route('/destinations/{id}', methods = ['GET'])
def destinations(id):
page = 'destinations/'+id+'.html'
return render_template(page)
How to route this pages properly?
There's only a slight mistake in your code. You have to change the curly braces of id in /destinations/{id} toas /destinations/<id> and then the code will work perfect...
I'd have a context processor, which maintains a dictionary of the required links:
#app.context_processor
def processor():
""" This function injects vars into every page """
cities = {
'paris':'Paris',
'newyork': 'New York',
'moscow':'Moscow',
'vegas':'Las Vegas',
'pataya':'Pataya'
}
return dict(CITIES=cities)
At some point you could build this dictionary from a database or other storage.
Then include in your template:
<li>Destinations
<ul class="nav-dropdown">
{% for city_id,city_name in CITIES.items() %}
<li>{{city_name}}</li>
{% endfor %}
</ul>
</li>
This url_for function, generates a URL like: /destinations/paris.
This method avoids having to manually write city names/ids into your template file.
Related
I am working on Cuckoo Sandbox and I hope that everyone will understand my question as It is going difficult for me to explain the question. I am working on different files and I am facing some issues. The issue is that I want to call dynamic variable in html anchor tag in Django, but, when I pass the dynamic variable the sidebar disappears automatically. Also I am working on Jinja template. I need your help guys:
urls.py file:
url(r"^(?P<task_id>\d+)/$", AnalysisRoutes.redirect_default, name="analysis/redirect_default"),
routes.py file:
#staticmethod
def redirect_default(request, task_id):
if not isinstance(task_id, (unicode, str)):
task_id = str(task_id)
return redirect(reverse(
"analysis",
args=(re.sub(r"\^d+", "", task_id), "summary")),
permanent=False
)
include.html file:
<li>
<a href="{% url 'analysis/redirect_default' 45 %}">
<div class="parent-icon"><i class='bx bx-home'></i>
</div>
<div class="menu-title">Summary</div>
</a>
</li>
In HTML file you can see that there is a int number 45. Just need to parse task_id dynamic value in html anchor tag. When I pass task_id variable in replace of 45 the sidebar disappears automatically. Kindly help me to resolve this issue. Thank you
When we pass static Id the Sidebar remains appears and nothing change
But when I pass dynamic data or without any Id the sidebar disappears as explained in figure 2:
without any Id or when pass dynamic data
Try get_absolute_url() instead of hard coding.
from django.urls import reverse
def get_absolute_url(self):
return reverse('analysis', kwargs={'id' : self.id})
{{ analysis.data}}
I'm trying to create a little job board, each job card is dynamically inserted from flask from a list (for now, will be an SQL DB later)
When the user clicks the button View Task I want it to go to another page where it displays the task/job in more detail.
What I want attached to that button is the job/task ID.
This is how I'd want/expect the button to function
{% for task in available_tasks %}
<div>
View Task
</div>
{% endfor %}
This could then be pasted into a route that would take the ID as an argument and fetch the full job/task information.
app.route('/view_task/<task_id>')
def view_task(task_id):
task_data = task_database[taskid]
return render_template('detailed_view.html', task_info=task_data)
My problem is href="url_for('view_task', task_id={{ task['id'] }})" doesn't work, is there a way to do this?
You generated the output href="url_for('view_task', task_id=<some id>)", so the literal text url_for(..) is in the HTML generated and delivered to your browser, and HTML engines don't recognise that string as a valid URL for anything.
You need to put the url_for() function in the {{ ... }} brackets, not just the task id value:
href="{{ url_for('view_task', task_id=task['id']) }}"
Only then is url_for() actually treated as an expression for Jinja2 to execute, at which point the HTML produces will contain the href="http://localhost:5000/view_task/<some id>" string.
I have a python list of urls which looks like this:
list = ['/abc/1',
'/abc/3',
'/abc/5',
...
]
all these urls follow the url pattern 'abc/n/' which I have defined in urls.py.
now I want to pass this list to a django template in such a way that each element of this list becomes clickable and runs the function corresponding to their url pattern on clicking them.
I tried hardcoding the anchor tag to each element of this list. for example
for i in range (len(list)):
list[i] = "<a href = \'" + list[i] + "\'>click me</a>"
and then sending this list to template via HttpResponse() function via a context. But this displayed the list items in raw text format instead of links
What is happening here is that django is being safe and escaping your html. This is to prevent XSS vunerabilities. This is a vulnerability that happens when e.g. users can insert malicious HTML into your site.
Now, you could turn that off, but then you risk having the aforementioned security issues.
What you will want to do instead is generate the html in the template, not in your code (that's what templates are there for):
{% for link in links %}
click me
{% endfor %}
Now you just need to pass links in your template context!
Instead of using a for loop format the links as html, just pass the list directly into your context then within your django template use:
{% for item in list %}
Link text
{% endfor %}
If your links are not all going to be placed on the page consecutively, you can just use:
{{ list.index }}
with index being the index of the item you wish to access
Sorry in advance if there is an obvious answer to this, I'm still learning the ropes with Django.
I'm creating a website which has 6 pre determined subjects (not stored in DB)
english, civics, literature, language, history, bible
each subject is going to be associated with a unique color.
I've got a template for a subject.html page and a view that loads from the url appname/subject/subjectname
what I need to do is apply particular css to style the page according to the subject accessed. for example if the user goes to appname/subject/english I want the page to be "themed" to english.
I hope I've made myself clear, also I would like to know if there is a way I can add actual css code to the stylesheet and not have to change attributes one by one from the back-end.
thanks very much!
In templates you can use conditionals for add css, like this:
<div class="{% if subject=='civics' %}civic-class{% endif %}"></div>
For this, subject value should come from view.
Now, for themed page, you could use the extends tag. Let's supose:
def your_view(request):
subject # Here you get the url subject, 'how' is up to you
if subject == 'english'
template_base = '/some/html/tenplate.html'
elif subject == 'civis':
template_base = '/some/other/template.html'
... # then you return 'template_base' variable to template
Then in template:
{% extends template_base %} # at the top
Hope this helps, is the same logic if you use Class-Based views.
Django's views are not responsible for the presentation, it's the template (and css etc of course)'s reponsability. Now assuming you have the same view serving different subjects, the view obviously need to know which is the current subject (I assume from a captured part of the url passed as argument to the view), so it can easily pass this information to the template, which in turn can use it to add a subject-specific class to the body tag. Then you only have to write your css accordingly.
As an example:
# urls.py
patterns = urlpatterns('',
#...
url(r'whatever/(P?<subject>[a-z-]+>)/$', 'myviews.index', ...),
)
# myviews.py
def index(request, subject):
# do whatever
context = {
# whatever else
'subject':subject
}
return render(request, "whatever/index.html", context)
# whatever/index.html
<html>
# headers etc
<body class="something {{ subject }} etc">
# whatever here
</body>
</html>
You can do this is many ways.
In general you need to return some variable from your view to the html and depending on this variable select a style sheet, if your variable name will match you style sheet's name you can do "{{variable}}.css", if not you can use JQuery.
I am brand new to web development, Django, python, html, etc. I have a basic Django app that displays a list of publication titles that have been entered into the database. This works fine.
I now want to make it so that each publication title is a link that - when clicked on - renders another template with the details of the publication that was clicked. So far, I know how to get the publication link to render a template, but I am trying to figure out how to pass in the publication title to the hyperlink so that the data that is rendered in the details template will be specific to the title that was chosen.
Here is what I have in my publication template which displays all the publications (it is incorrect, but hopefully it clarifies what I am trying to do):
<html>
<head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li><strong>{{ publication.title}}</strong></li>
{% endfor %}
</ul>
</body>
</html>
For the sake of context, the url pattern that handles this is:
url(r'^(?P<detail_type>\w+)/(?P<link_item>\w+)/detail$', get_details)
And the view function is:
// note: I may have some of the logic/syntax wrong here, but this is the basic idea
def get_details(request, detail_type=None, link_item=None):
if detail_type == "publications":
publication = Publication.objects.filter(title__iexact=link_item)
return render(request, 'publication_detail.html', {'detail_type' : detail_type, 'publication' : publication})
elif ....
Like I said, I am very much a beginner so if I am approaching this in wrong way, any suggestions or resources are appreciated. Thanks.
If you use named url patterns you can easily do this with the url template tag.
urls.py
url(r'^(?P<detail_type>\w+)/(?P<link_item>\w+)/detail$', get_details, name='details')
template
{% url 'details' 'publications' publication.title %}
I hope you know about SlugField too, it is much better for urls than a normal CharField.
An alternative:
urls.py
url(r'^(?P<detail_type>\w+)/(?P<pk>\w+)/detail$', get_details, name='details')
template
{% url 'details' 'publications' publication.pk %}
views.py
def get_details(request, detail_type=None, pk=None):
if detail_type == "publications":
publication = Publication.objects.get(pk=pk)
return render(request, 'publication_detail.html', {'detail_type' : detail_type, 'publication' : publication})
elif ....
This uses the primary key of the entry instead of the title. If you want to have a url with the title in it you will want to add and use a SlugField on your model.
This looks pretty good to me, although you may want to use get as opposed to filter in your view function if all the publication titles are unique and you want to pass an instance of Publication rather than a queryset (containing one item) into the detail template. This would throw an error of there were 0 or >1 matches, but it sounds like that's probably the behavior you'd want
However, I'm not sure what it is that you're missing here. What does publication_detail.html look like? You should have essentially everything you need in the above code to render the details, assuming they're all contained in the relevant Publication instance.