I am doing a website by Django, but I have a registor problem, which I don't know how to solve,help me).Before last change, everything was working, but I have changed only this files:
views.py
from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect
from django.shortcuts import render
from django.http import *
from django.template.response import TemplateResponse
def index(request):
cat = ["Cars", "Plates", "Fruits", "Phones", "Bananas"]
return render(request, "firstapp/index.html",
context={"cat": cat})
def about(request):
return HttpResponse("About")
def contact(request):
return HttpResponseRedirect("/about")
def details(request):
return HttpResponsePermanentRedirect("/")
index.html
{% extends "firstapp/base.html" %}
{% block title %}Index{% endblock title %}
{% block header %}Main page{% endblock header %}
{% block content%)
<p>Types of products</p>
{% for i in cat %}
<li>{{ i }}</li>
{% endfor %}
{% endblock content %}
and I got this problem:
Invalid block tag on line 9: 'endblock'. Did you forget to register or load this tag?
1 {% extends "firstapp/base.html" %}
2 {% block title %}Index{% endblock title %}
3 {% block header %}Main page{% endblock header %}
4 {% block content%)
5 <p>Types of products</p>
6 {% for i in cat %}
7 <li>{{ i }}</li>
8 {% endfor %}
9 {% endblock content %}
I don't know what to do in this case, help me!
Your problem is on line 4. There is a mistake there. You are using
{% block content%)
instead of
{% block content %}
Note: It should be } instead of )
Related
I am trying for hours on a problem that seems very straightforward and I tried everything but it's not working. I want to display the list of blogs on a template. So, I have these views:
from django.views import generic
from .models import Blog
class BlogList(generic.ListView):
queryset = Blog.objects.filter()
template_name = 'table_of_contents.html'
context_object_name = 'blog_list'
class BlogDetail(generic.DetailView):
model = Blog
template_name = 'blog.html'
And this is the table_of_contents.html template where I want to display the list of blogs:
{% block table_of_contents %}
<p>Just for test</p>
{{ blog_list }}
{%endblock table_of_contents %}
I expect that would display the queryset string on the frontend, but that's not the case. I debugged BlogList using the Django shell and made sure that queryset was not empty.
What is the (obvious) detail that I am missing here?
Edit:
Here is the blog.html template as well:
{% extends 'base.html' %}
{% block content %}
<p>Test blog template</p>
{% block table_of_contents %} {% include 'table_of_contents.html' %} {% endblock table_of_contents %}
{% endblock content %}
Again, the querystring is not rendered at all, no matter if I use a for loop or just do {{ blog_list }}.
The queryset will look like <QuerySet …>, so that will not render effectively on the page, since it will assume that <QuerySet as a HTML tag, not as content.
What you can do is iterate over the items in the QuerySet, and render these individually, for example:
{% block table_of_contents %}
<p>Just for test</p>
{% for blog in blog_list %}
{{ blog.title }}
{% endfor %}
{%endblock table_of_contents %}
of course here I assume that your Blog model has a title field. You might need to change this to a different field.
If you define a {% block … %} [Django-doc] this means you need to inherit from a parent template that specifies a block with the name table_of_contents.
You thus need to have a parent template, for example:
<!-- parent.html -->
My page
{% block table_of_contents %}
{% endblock table_of_contents %}
in your template where you list the blogs, you thus then inherit from the parent.html page with the {% extends … %}:
{% extends 'path/to/parent.html' %}
{% block table_of_contents %}
<p>Just for test</p>
{% for <b>blog in blog_list</b> %}
{{ blog<b>.title</b> }}
{% endfor %}
{%endblock table_of_contents %}
What's happening. I still have trouble displaying the title list. Where did I make a mistake?
views.py
def dashboard(request):
bookmarks = Text_field.objects.order_by('created')
return render(request, 'Site/dashboard.html', {'bookmarks': bookmarks})
def about_me(request, pk):
about = get_object_or_404(Text_field, pk=pk)
return render(request, 'Site/about_me.html', {'about': about})
dashboard.html
{% extends "Site/base.html" %}
{% load static %}
{% block content %}
{% for bookmark in bookmarks %}
<div>
<p>Stworzono dn. {{ bookmark.created }}</p>
<h1>{{ bookmark.title }}</h1>
<p>{{ bookmark.text|linebreaksbr }}</p>
</div>
{% endfor %}
{% endblock %}
urls.py
path('', views.dashboard, name='dashboard'),
path('about/<int:pk>/', views.about_me, name='about_me'),
In your comment you say you defined an app_name in your urls.py, you need to include that app_name in your {% url ... %} name:
{% url 'Site:about_me' pk=bookmark.pk %}
I have a page that shows a list of objects and I want to add a button beside each one to enable the user to delete it. I've found a few questions about similar scenarios online but for some reason I keep getting errors when I try to replicate the solutions.
Here is the delete function in views.py:
def delete_dish(request, pk):
query = Dish.objects.get_object_or_404(pk)
supermenu = query['supermenu'].pk
query.delete()
return redirect('foodmenu:menu', supermenu)
Here is the form in the HTML template:
{% if not supermenu.dish_set.count == 0 %}
<ul>
{% for dish in supermenu.dish_set.all %}
<li>
{{ dish.name }} - {{ dish.description }}
<form action="{% url 'foodmenu:delete_dish' dish.id %}" method="POST">
{% csrf_token %}
<button type="submit">X</button>
</form>
</li>
{% if not dish.price_set.count == 0 %}
<ul>
{% for price in dish.price_set.all %}
{% if price.label %}
<li>{{ price.label }}: {{ price.cost }}</li>
{% else %}
<li>{{ price.cost }}</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>No dishes on this menu!</p>
{% endif %}
And here is the urls.py:
app_name = 'foodmenu'
urlpatterns = [
...
path('dish/delete/<int:dish.id>', views.delete_dish, name="delete_dish")
]
When I click the button, the browser goes to ./dish/delete/1 (1 being the pk of the object), but Django returns a 404 error.
Instead of query = Dish.objects.get_object_or_404(pk) you should use get_object_or_404 shortcut:
from django.shortcuts import get_object_or_404
from django.views.decorators.http import require_POST
#require_POST
def delete_dish(request, pk):
if request.method
query = get_object_or_404(Dish, pk=pk)
supermenu = query.supermenu.pk
query.delete()
return redirect('foodmenu:menu', supermenu)
Also change your url pattern to this:
path('dish/delete/<int:pk>/', views.delete_dish, name="delete_dish")
UPD
As #daniherrera mentioned in his comment, you probably want to check request's method, to prevent accidental deletions. For this you can use require_POST decorator.
For some reason this is returning nothing for me, no errors but just not getting anything returned by the tag.
taglist.py
from django import template
from article.models import Tag
register = template.Library()
#register.assignment_tag
def taglist():
return Tag.objects.values('name').distinct()
base.html
{% load taglist %}
{% block sidebar %}
<ul>
<li>Articles</li>
<li>Admin</li>
{% taglist as mytags %}
{% for t in mytags %}
<li>{{t}}</li>
{% endfor %}
</ul>
{% endblock %}
Any idea whats happening here? The same Tag.objects.... returns what I want it to when done in the shell.
How do you get Django comments to redirect back to the same page where you're filling out a comment if there are errors in the comment submission form?
So basically I have a template like this:
{% block content %}
{% render_comment_form for show %}
{% get_comment_count for show as comment_count %}
<div id="comments-count">
{% if comment_count == 0 %}
No comments yet. Be the first!
{% else %}
Number Of Comments: {{ comment_count }}
{% endif %}
</div>
{% if comment_count > 0 %}
{% render_comment_list for show %}
{% endif %}
{% endblock %}
I created my own list.html and form.html and everything looks fine. In the form.html template there is some code like this:
<ul class="form-errors">
{% for field in form %}
{% for error in field.errors %}
<li>{{ field.label }}: {{ error|escape }}</li>
{% endfor %}
{% endfor %}
</ul>
So obviously, if there is an error in the comment submission form, I would like the user to see the same page as before only with some errors displayed in the comments form. Or alternatively if this is not possible, just ignore the error and instead of transitioning to the preview.html template, it would just not save the comment and again go back to the page.
Any help? Note ideally I dont want to have to create a custom comments app. This functionality should be there already. I know there's a next variable you can pass (and I am doing this), but it only works if the comment form is successful.
you have to use HttpResponseRedirect
from django.http import HttpResponseRedirect
def comment_form(request):
error = request.GET.get('error', None)
requestDict = {'error': error}
return render_to_response('comments.html', requestDict, context_instance=RequestContext(request))
def post_comment(request):
....
your code
....
if something_goes_wrong:
HttpResponseRedirect('project/comment_form/?error=ThereisProblem')
And in template you can do this:
{If error %}
<h1>{{error}}<h1>
{%else%}
render comments...
{%endif%}
Hope this will help you :)