python - Random parameters amount in {% url %} tag in Django templatess -
i want apologize bad english. (i'm russia)
so, want add pagination on site. imagine, have 2 pages: site.com/ (as main page), on there posts list, , site.com/tag/tagname - posts list tag.
both pages must have paginator. want create individual template pagination block paginator.html , include in index.html , tag.html.
page urls must looks like: site.com/5 , site.com/tag/tagname/5. so, have different amount of parameters {% url %} tag: 1 main page , 2 tag page.
just simple example:
urls.py:
app_name = 'test_app' urlpatterns = [ url(r'^$', views.index, name = 'index'), url(r'^(?p<page_num>[0-9]+)$', views.index, name = 'index'), url(r'^tag/(?p<tag_name>[-\w]+)$', views.tag, name = 'tag'), url(r'^tag/(?p<tag_name>[-\w]+)/(?p<page_num>[0-9]+)$', views.tag, name = 'tag'), ] views.py
def paginate(objects_list, in_page, page_num): paginator = paginator(objects_list, in_page) page = paginator.page(page_num) return page def index(request, page_num = 1): page = paginate(questions, 5, page_num) return render(request, 'test_app/index.html', { 'questions': page, }) def tag(request, tag_name, page_num = 1): page = paginate(questions, 5, page_num) return render(request, 'test_app/tag.html', { 'questions': page, 'tag_name': tag_name, }) questions list of questions.
index.html
{% question in questions %} author: {{ question.author }} <br> title: {{ question.title }} <br> body: {{ question.body }} <br> {% endfor %} {% load i18n %} {% trans "test_app:index" page_url %} {% include "./paginator.html" %} tag.html
posts tag: {{ tag_name }} <br> <br> {% question in questions %} author: {{ question.author }} <br> title: {{ question.title }} <br> body: {{ question.body }} <br> {% endfor %} {% load i18n %} {% trans "test_app:tag" page_url %} {% trans tag_name var1 %} {% include "./paginator.html" %} and paginator.html
{% if questions.has_previous %} <a href = "{% url page_url 1 %}"><button> << </button></a> <a href = "{% url page_url var1 questions.previous_page_number %}"><button> {{ questions.previous_page_number }} </button></a> {% endif %} <a><button> {{ questions.number }} </button></a> {% if questions.has_next %} <a href = "{% url page_url var1 questions.next_page_number %}"><button> {{ questions.next_page_number }} </button></a> <a href = "{% url page_url var1 questions.paginator.num_pages %}"><button> >> </button></a> {% endif %} i tried run example. it's works tag.html.
for index.html have message:
noreversematch @ /test/ reverse 'index' arguments '('', 2)' , keyword arguments '{}' not found. 2 pattern(s) tried: ['test/(?p[0-9]+)$', 'test/$']
and quesion. how can use {% url %} tag, inside of tamplate paginator.html, if don't know, how many parameters current url contain?
also, want use variable name instead of "questions" in paginator.html.
i tried this:
in tag.html , index.html write: {% trans question page_object %}
and use "page_object" instead of "questions" in paginator.html. have error message:
'page' object has no attribute 'replace'
{% trans question page_object %}
i tried several variants, , don't work good. dont't know, can yet.
i don't want paste paginator in each page.
Comments
Post a Comment