I am trying to develop a quiz app in django class base view. i get all questions in questions and render to template. i want access one by one questions using html next button. so suggest me any backend code required or by using javascript we can iterate?
View.py
thanks for posting.
{% for question in questions %}
Do stuff with each question
{% empty %}
Do stuff when there's no questions
{% endfor %}
Related
I am making a blog website and I have to add comments to my post for that I have to check which post the comment is associated with and I am writing this line
{% if {{comment.post}}=={{blog.post}} %}
I know this line won't work because {%%} and {{}} cannot be used in the same line so does anyone has a solution for this.
In a template tag, you do not use the double curly brackets, but just the variables:
{% if comment.post == blog.post %}
…
{% endif %}
That being said, please do not filter in a template. Business logic belongs to the models or views. If you use relations, like a ForeignKey [Django-doc], or a ManyToManyField [Django-doc], then Django adds managers to the corresponding models. This also means it fetches the related comments by a query to the database. Especially if the number of comments will increase, eventually filtering in the template will no longer be feasible.
{% if comment.post == blog.post %}
…
{% endif %}
this will work well
I am utilizing an includes within my template, but would only like to send a splice of the django queryset through the template. I don't want to splice in my view, because this is apart of a larger for loop that will be continually calling the include with different subsections. Here was my wishful thinking:
{% for g in gl %}
{% include 'includes/file.html' with ps=ps|id:g.id %}
{% endfor %}
You should create a filter tag that will receive your queryset and return a filtered one.... you can find details here:
https://docs.djangoproject.com/en/2.1/howto/custom-template-tags/
I am trying to customize the output of the comments list and forms using Django's comments framework.
Inside my own template, should I try to customize the comments by doing something like {% get_comment_form for object as form %} and carefully construct the form based on the form variable or should I override the form.html template and simply call {% render_comment_form for object %}?
Currently I'm leaning more toward using {% get_comment_form for object as form %} inside my own template, but using form.html as a guideline to write my own form.
On my websites, overriding form.html (and other comments templates) and simply calling {% render_comment_form for object %} works very well.
You should definitively have a form.html that is customized for your website's design. And in case you want a comment form to have a particular design, you can use get_comment_form.
I'm using Django to show a list of posts. Each post has a 'is_public' field, so if one post's 'is_public' equals to False, it should not be shown to the user. Also, I want to show a fixed number of posts in one page, but this number can be changing depending on views.
I decided to crop the queryset in template as a few views are using the same template, generating it in the view means a lot of repeated codes.
If written in python, it should look like this:
i=number_of_posts_to_show_in_one_page
while i:
if qs[i].is_public == True:
#show qs[i] to the page
i--
As the django template does not support while loop and for loop seems hard to control, is there a way of achieving this? Or should I do it in another way?(One idea is to crop the qs before looping)Thanks!
Update:
I've written this template tag to pre-process the queryset:
#register.simple_tag(takes_context=True)
def pre_process_list(context,list,numbers):
#if not user.has_perm('admin'):
context['result_list']=list.filter(is_public=True, is_removed=False)[0:numbers]
#else:
#context['result_list']=list[0:numbers]
return ''
Before using for loop in the template, I'll pass the queryset to this templage tag, and use a simple for loop to show its result.
If in the future I want to show non-public posts to admins(which is not decided yet), I can write in some logic like the commented ones, and have them styled differently in the template.
{% for post in posts %}
{% if post.is_public %}
{{ post }}
{% endif %}
{% endfor %}
Though this would be a perfect use case for a manager.
You could write a simple manager that filters public posts.
class PublicPostManager(models.Manager):
def get_query_set(self):
return super(PublicPostManager, self).get_query_set().filter(is_public=True)
Then you would add it to your Post Class:
class Post(models.Model):
...
public = PublicPostManager()
Then you could pass post.public.all() as public_posts to your template and simplify your loop:
{% for post in public_posts %}
{{ post }}
{% endfor %}
#arie has a good approach with the manager, but you can easily do the same without writing a manager:
# View
posts = Post.objects.filter(is_public=True) # or use the manager
# Now, you can either limit the number of posts you send
# posts = posts[:5] (only show five in the view)
return render_to_response('foo.html',{'posts':posts})
# Template
# Or you can do the limits in your template itself:
{% for post in posts|slice:":5" %}
{{ post }}
{% endfor %}
See the slice filter on more information.
However, since this is a common operation, with django 1.3 you can use class based views to automate most of this.
I am using django-threadedcomments. Everything works fine except 2 things: csrf token and user template tag.
Problem is, when user submits a comment, there is no csrf token for the form, so the form could not be validated server-side. Tried adding csrf token to the dictionaries that threaded-comments passes internal with no result; kept receiving errors (most of them telling that this-method takes only 2 arguments with 3 given). Tried to fix those methods to accept 3 arguments and just pass third one further; no success.
Did someone stumble upon the same problem in past and solved it? because this is not an acceptable solution for me:
MIDDLEWARE_CLASSES = (
#'django.middleware.csrf.CsrfViewMiddleware',
)
Second one - there is a HTML helper to get the user_id for the user who posted a comment. Is there an out of the box html helper to get the name of the user by id or would i have to write it myself?
http://code.google.com/p/django-threadedcomments/
Here is the code for the project, I cant really tell exactly which chunks of it should be posted here so I just give link to the entire project.
I am really stuck in here and any help would be welcomed.
Thanks in advance.
Tried adding csrf token to the
dictionaries that threaded-comments
passes internal with no result;
csrf_token is a template tag -- it shouldn't be passed as an argument somewhere.
I took a look at threadedcomments and it's based on contrib.comments with no html rendering, so it's up to you to insert the csrf_token in your template.
What does your TEMPLATE code look like that is displaying your form code?
If you have CsrfViewMiddleware enabled and you are using RequestContext in your view, you simply need to add {% csrf_token %} inside of your <form></form> tags.
As for getting the user name:
ThreadedComment is a subclasses of Comment which has a name property, or you could just access the User directly...
{% for comment in comments %
{{ comment.user.first_name }}
{{ comment.name }}
{% endfor %}
You should use {% csrf_token %} tag or #csrf_protect in a views
You can put your form in its own template and {% include %} it into your page template. As of Django 1.3, {% include %} can pass context variables to the included template. Here's what I'm using with django.contrib.comments instead of a templatetag:
...
{% include "comments/comment-form.html" with content_object=article user=request.user %}
...
{%csrf_token %} works in this included template because it's using your main view context.