I am very new to web development and I have created a sample project using Django. So far I have a Django powered page that displays the contents of one of my database's model objects which is called Publications. The code I have in my view template is:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% endfor %}
</ul>
</body></html>
This works fine, but now I would like to access and display a many to many attribute on Publications called Tags. I have tried adding another for tag as follows:
<html><head><title>Publications</title></head>
<body>
<h1>Publications</h1>
<ul>
{% for publication in publication_list %}
<li>{{ publication.title }} </li>
{% for tag in publication_list.tags %}
<li>{{ tag.title }} </li>
{% endfor %}
{% endfor %}
</ul>
</body></html>
I realize this is quite wrong, but I don't see how to access the Tags model. For reference, my function for displaying the publications in the view is:
def display_publications(request):
publication_list = Publication.objects.order_by('title')[:10]
return render(request, 'publications.html', {'publication_list': publication_list})
And my Publications and Tag Models are:
class Tag(models.Model):
title = models.CharField(max_length=50)
class Publication(models.Model):
title = models.CharField(max_length=200)
tags = models.ManyToManyField(Tag, blank=True)
Any help is appreciated.
What you are doing only accesses the ManyRelatedManager. You need to specify a query against that manager. In python, it would be:
publication.tags.all()
In a django template it would be:
{% for tag in publication.tags.all %}
{{ tag }}
{% endfor %}
This should be covered in the official documention on many-to-many relationships.
Edit: Here's a good example of how many-to-many relationships work: https://docs.djangoproject.com/en/1.5/topics/db/examples/many_to_many/
Because you seem to be having some trouble with this, given your comments on the other question, here are the changes to the template. You do not need to modify the view at all from what you have given above.
{% for publication in publication_list %}
<li>{{ publication.title }}
<ul>
{% for tag in publication.tags.all %}
<li>{{ tag.title }} </li>
{% endfor %}
</li>
</ul>
{% endfor %}
Related
I want to create a list of entries in which each entry is linked to its page like this: wiki/entry-title. I'm using a for loop to add <li>s to HTML. here's the code:
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
urlpattern:
path('wiki/<str:title>', views.entry, name='entry')
what should I type in href to link the <li> to wiki/entry?
You can set your value in url as
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
Its better to use {% url %} [Django-doc] template tags as
<ul>
{% for entry in entries %}
<li>{{ entry }}</li>
{% endfor %}
</ul>
NOTE : change value with your value accordingly. For e.g. {{entry.value}} or {{entry.title}} or {{entry.id}}
There is a clear example in the django docs
<ul>
{% for yearvar in year_list %}
<li>{{ yearvar }} Archive</li>
{% endfor %}
</ul>
In django, the template parser always handles django code first, then html/javascript. So you would insert a django variable into an anchor tag the same way you'd insert it anywhere in the template and the parser will replace it before it tries to render the html. If it's a django url, you can use the {% %} format as referenced in the previous answer, and if it's a url that's perhaps stored on the object, you can just use {{ }} (like {{ entry.wiki_url }}). You can also use text for some of the url and a variable for part. So if you have a wiki site that has a base url of, for instance, https://mywiki.com you'd write the href like:
<ul>
{% for entry in entries %}
<li>{{ entry.title }}</li>
{% endfor %}
</ul>
I found out that the only solution that was working for me was this format
{{ x }}
x: this is variable that we want to implement inside href attribute
'url_path_name' : this is the name of the url we gave in urls.py , see :
path("wiki/<str:name>" , views.entry, name="url_path_name")
So this defined url should look like this:
wiki/ ?
href="{% url 'url_path_name' x %}"
{% extends "layout.html" %}
{% block heading %}
Search Results
{% endblock %}
{% block body %}
fgrgrgtgtg
<ul>
{% for book in books %}
<li>
{{ books.title }}
</li>
{% endfor %}
</ul>
{% endblock %}
#app.route("/search", methods=["GET", "POST"])
def search():
# Query database for book title
books = db.execute("SELECT * FROM books WHERE title = :title",
{"title": request.form.get("title")}).fetchall()
print (books)
return render_template("search.html", books=books)
I'm working on a project for CS50W and I'm stuck. I'm trying to implement a very bare-bones search function that should find all books in my database by title, and then render them on search.html. I just want to get it to work in the simplest way possible before I start adding more complexities to it.
I added "print(books)" in my python code just to make sure that I was successfully pulling the data from the database, and it appears that that is working properly, as it prints the expected results in my terminal. But nothing appears in my html on search.html.
You have a typo; the variable within your loop is book, not books.
{% for book in books %}
<li>
{{ book.title }} # here
</li>
{% endfor %}
i'm new in Django developing.
I'm following the tutorial about Library on MDN (https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django)
Until i follow the code all work but i'm trying implement author page by myself. Probably is very stupid issue but is one day that i'm turning around like a dog with its tail.
There is 2 page: author_list and author detail.
I set urls.py (in my project) i set view.py and crate my template.
I follow the same step of tutorial for realize book_list and book_detail but when i click on my author the page don't go to the detail of that author and stay in author_list.html.
Here the code urls.py :
path('authors/', views.AuthorListView.as_view(), name='authors'),
path('author/<int:pk>', views.AuthorDetailView.as_view(), name='author-detail'),
Here views.py:
class AuthorListView(generic.ListView):
model = Author
class AuthorDetailView(generic.ListView):
model = Author
Here author_list.html with link get_absolute_url:
{% extends "base_generic.html"%}
{% block content %}
<h1>Author list</h1>
{% if author_list %}
<ul>
{% for aut in author_list %}
<li>{{ aut.first_name }} - {{ aut.last_name }}</li>
{% endfor %}
</ul>
{% else %}
<p>There are no author.</p>
{% endif %}
{% endblock %}
Here author_detail.html:
{% extends "base_generic.html" %}
{% block content %}
<h1>Author</h1>
{% if author %}
<p><strong>Nome: </strong> {{ author }}</p>
<p><strong>Nato il : </strong> {{ author.date_of_birth }}</p>
<p><strong>Morto il : </strong> {{ author.date_of_death }}</p>
{% endif %}
{% endblock %}
Here the screenshot
Author_list.html before click url=catalog/authors/
After click url change but page not
Thank to all for help
I believe you need DetailView instead of ListView for AuthorDetailView.
Looks like a typo to me, you want generic.DetailView (instead of ListView) for the author/<int:pk> path.
I also don't think it's right to extend base_generic for the template for the detail view. But that depends exactly what is in this base template.
I have a Pelican blog. I want to call the external links list programmatically, rather than hard code them into the template. For example, the blog post categories are called programmatically, e.g.,
{% for category, articles in categories[0:10] %}
<li>{{ category }}</li>
{% endfor %}
</div>
<div class="l-box pure-u-1-3">
{% for category, articles in categories[11:20] %}
<li>{{ category }}</li>
{% endfor %}
</div>
<div class="l-box pure-u-1-3">
{% for category, articles in categories[21:30] %}
<li>{{ category }}</li>
{% endfor %}
So to be clear, I am looking to change this code to call from a single file which lists some external weblinks.
Assign them to the LINKS variable in your pelicanconf.py e.g.
LINKS = (
('my link 1', 'http://some.link.here'),
('my link 2', 'http://some.other.link.here')
)
and then call them in your template with
{% for name, link in LINKS %}
{{ name }}
{% endfor %}
All variable defined in your pelicanconf.py, as long as they are in all-caps, can be accessed in your templates.
See: http://docs.getpelican.com/en/3.5.0/themes.html#templates-and-variables
I have a one-to-many relationship between restaurants and photos. I am having trouble displaying a single image for a corresponding restaurant.
Right now I get all of the restaurants like this:
restaurants = Restaurant.objects.all()
In my template I have a loop that goes through the restaurants and displays information. However, I am unsure how to retrieve an image that is associated with the restaurant:
{% for restaurant in restaurants %}
{{restaurant.name}}
{{restaurant.address}}
{{CODE TO DISPLAY IMAGE}}
...
{% endfor %}
How can I retrieve the first image corresponding to the restaurant? I have tried restaurant.restaurantphoto_set.get.image, but this only works when there is a single image.
It's a little hard to diagnose without knowing what your model looks like, but have you tried:
restaurant.restaurantphoto_set.all()
The docs have some pretty solid examples here:
https://docs.djangoproject.com/en/1.7/topics/db/examples/many_to_one/
do in your template
{% for photo in restaurant.restaurantphoto_set.all %}
{{ photo.image }}
{% endfor %}
This is how I ended up solving my problem:
{% for restaurant in restaurants %}
{{restaurant.name}}
{{restaurant.address}}
{% for photo in restaurant.restaurantphoto_set.all %}
{% if forloop.counter == 1 %}
<img src= "{% static photo.image %}" class="img-curved">
{% endif %}
{% endfor %}
{% endfor %}