django-templates

Django - Paginating in Template using multiple GET parameters

心不动则不痛 提交于 2021-02-18 15:36:49
问题 I am using a Django Paginator and I want to have multiple available get parameters, such as: page=1 sort_by=price However, in my template tags I have: Showing items sorted by {{ SORT_PARAM }}. Showing {{ ITEMS_PER_PAGE }} items per page. {% if has_prev %} <a href="?page={{ prev_page }}">Previous</a> | {% endif %} However, this does not preserve the other GET variables. What I mean is, if I'm viewing page/?page=1&sort_by=price and I click the link in the template fragment above, I will go to

Django partial template responses

对着背影说爱祢 提交于 2021-02-18 08:15:32
问题 Is it considered correct/ are there any pitfalls in returning partial templates to ajax POST requests? For example: if request.is_ajax: # response is just the form return render(request, 'contact/fields.html', {'form':form}) 回答1: The most typical approach is returning JSON and then contructing whatever HTML you need client-side from the JSON data. However, it could be argued that this is mixing presentation with behavior and it would be better to clearly separate out the HTML. On the flip

I'm getting a NoReverseMatch error in my home page

◇◆丶佛笑我妖孽 提交于 2021-02-11 16:55:49
问题 I'm getting a NoReverseMatch error in my home page. It is from the html that I injected from my announcement app. It says that the reverse of the link is cannot be found. When I removed that line It shows the card but the text with template tag. _announcement_home.html : <div class="container"> <div class="card announcement-card" style="width: 18rem;"> <h5 class="card-header">Announcement</h5> <div class="card-body"> <h5 class="card-title">{{announcement.title}}</h5> <p class="card-text">{

I'm getting a NoReverseMatch error in my home page

末鹿安然 提交于 2021-02-11 16:54:25
问题 I'm getting a NoReverseMatch error in my home page. It is from the html that I injected from my announcement app. It says that the reverse of the link is cannot be found. When I removed that line It shows the card but the text with template tag. _announcement_home.html : <div class="container"> <div class="card announcement-card" style="width: 18rem;"> <h5 class="card-header">Announcement</h5> <div class="card-body"> <h5 class="card-title">{{announcement.title}}</h5> <p class="card-text">{

Django 3.0: Reverse for 'product' with no arguments not found. 1 pattern(s) tried: ['product/(?P<slug>[^/]+)/$']

痞子三分冷 提交于 2021-02-11 16:01:47
问题 I have model named Book in models.py file. And this model has slug field to display details of books Books are being displayed in home.html template and product.html template is to display details of selected book. I really don't know much about slugs, and how they work. Models.py: class Book(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField('Title', max_length=255) authors = models.ManyToManyField(Author, related_name='books

Django 3.0: Reverse for 'product' with no arguments not found. 1 pattern(s) tried: ['product/(?P<slug>[^/]+)/$']

亡梦爱人 提交于 2021-02-11 15:59:40
问题 I have model named Book in models.py file. And this model has slug field to display details of books Books are being displayed in home.html template and product.html template is to display details of selected book. I really don't know much about slugs, and how they work. Models.py: class Book(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField('Title', max_length=255) authors = models.ManyToManyField(Author, related_name='books

Passing django template variables to js datatables

て烟熏妆下的殇ゞ 提交于 2021-02-11 15:40:41
问题 Hi I would like to create table using JS datatables library. I got problem when passing value in template to js script. I created JSON object from my table which I want to display. It's passed correctly to template, when I display it everything is fine, but when trying to pass it to script nothing happend and I got empty table. Thats the way I do it: class VotesList(generic.ListView): model = Vote template_name = 'votes-list.html' def get_context_data(self, **kwargs): votes = Vote.objects.all

No User matches the given query. Page Not found 404?

馋奶兔 提交于 2021-02-11 14:59:53
问题 In my blog website I want to open another user's profile on clicking one of the links. But it keeps on showing 404 error saying No user matches the given query. Here's that link in a base.html <a href="{% url 'blogapp:userprofile' username=view.kwargs.username %}">{{ view.kwargs.username }}</a> Here's my urls.py pattern for the function- path('userprofile/<str:username>/',views.userprofile,name='userprofile'), Here's my function views.py @login_required def userprofile(request,username): user

How to get related field's value as ManyToMany fields' choice label in Django

余生长醉 提交于 2021-02-11 12:24:22
问题 I have two related models with one of the fields of a model having ManyToManyField relationship with the other model like shown here: Models class Processes(models.Model): x_process = models.CharField(primary_key=True, ...) x_process_text = models.CharField(max_length=35, verbose_name='Short Desc') class RelStatuses(models.Model): rel_status = models.CharField(primary_key=True, ...) rel_status_text = models.CharField(max_length=55, verbose_name='Short Desc') app_process_compo = models

Cannot use filter inside Django template html

核能气质少年 提交于 2021-02-11 08:11:02
问题 I have an issue on my Django project. I have a situation as follows: {% for subObject in mainObject.subObjects.all %} This works nice, every subObject gets iterated nicely. What I want now is that I print a subset of the objects, something like: {% for subObject in mainObject.subobjects.filter(someField=someValue) %} So far I have searched solutions about the error I get: Could not parse the remainder: '(someField=someValue)' but did not find a solution about how the line should be different