page not found in django? - python

i am new to django. this is the error i keep getting when i try to visit node.name page.
page not found Request URL: http://127.0.0.1:8000/Category//
urls.py
Category/<category_id>/ [name='productlisting']
index.html
{% load mptt_tags %}
<ul class="root">
{% recursetree listing %}
<li>
{{ node.name }}
</li>
{% endrecursetree %}
</ul>
urls.py
path('Category/<category_id>/',views.productlisting, name='productlisting'),
path('Category/<category_id>/product/<product_id>/',views.details, name='details'),
thanks in advance.

First you have an extra ending / in the endpoint you are trying to hit.
This is not how we name urls in anchor tag. Please refer here on how to name url and how to use it in template
so basically idea is that you would name url in urls.py in following manner:
path('Category/<int:category_id>/',views.productlisting,name='productlisting'),
then you would use this url name in template as:
{{ node.name }}

Related

Django Template changing html attribute based on slug and current request

When I tried to iterate through a list of post titles to create links in a django template the urls never match the requested path (matching the current web page should alter the formatting on the page for that list item). When I try and change things, Django spits out a 404 error which doesn't let me diagnose the problem or it "works" but not as expected.
the relevant url patterns (in blog app within project):
path('', views.index, name='blog_index'),
path('<slug:post>', views.blog_post, name='entries')
the relevant views functions (in blog app within project):
ef index(request):
try:
return render(request, 'blog/index.html', {'posts':blog_posts.keys})
except:
raise Http404()
def blog_post(request, post):
try:
return render(request, 'blog/post.html', {
'post_title':post,
'post_content':blog_posts[],
'posts':blog_posts.keys
})
except:
raise Http404()
the navigation template:
<ul>
{% for post in posts %}
{% url "blog:entries" "{{post}}" as blog_entry %}
<li><a href="/blog/{{post}}" {% if request.path == blog_entry %} class="active"{% endif %}>
<span class="item">{{post}}</span>
</a></li>
{% endfor %}
</ul>
I suspect the {% url "blog:entries" "{{post}}" as blog_entry %} in the template is not resolving correctly as when I replace the href with {% blog_entry %} it causes a 404 error.
I have tried hard coding it in, I have tried different ways of writing the url, I have tried changing the syntax (in case I am doing it incorrectly. I have checked the page source when I have run tests. I have never been able to get the class attribute to be "active" when on the matching page.
EDIT: The links all go to the correct pages, however the IF logic in the template does not work. i.e. None of the changes I have tried make result in reques.path == blog_entry resolving as True.
Also the above code snippets are part of an App within a project and I have included more of the views.py and urls.py file. Currently I have the "blog posts" stored in a dictionary at the top of views.py for testing.
Try this:
<ul>
{% for post in posts %}
{% url "blog:entries" post=post as blog_entry %}
<li><a href="/blog/{{post}}" {% if request.path == blog_entry %} class="active"{% endif %}>
<span class="item">{{post}}</span>
</a></li>
{% endfor %}
</ul>

How to link an ID generated URL to the <a href> tag

I am making my own portfolio website using Django.. So the idea is: Make posts containing websites that i've developed. All these posts will be displayed in a page called "Projects" and then you can access a single project to read about it. Every project i upload have an ID, so i used this code to acess each project page:
# Single project page
path('projects/<int:post_id>/', views.project, name='project'),
Then in my PROJECTS page i have a simple(for now) HTML code to show all my projects and link them to the single project page. What i want to know is how to LINK this ID generated URL in this code:
<ul>
{% for project in projects %}
<li>{{ project }}</li>
{% empty %}
<li>No project have been added yet.</li>
{% endfor %}
</ul>
I tried using:
<li><a href="{% url 'main_portfolio/projects/<int:post_id>/' %}</a></li
And some other things but nothing i try is working.
Maybe try <a href="{{ project.get_absolute_url }}">, or
<a href="projects/{{ project.id }}/">
You can work with the {% url … %} template tag [Django-doc]:
<li>some text</li>

Clean url in django app. without ?page=1 pattern

I want to have my url pattern like the below pattern:
host:8000/archive/2/
I define page_kwarg in my view but I still receive: host:8000/en/2
Code form main url.py file:
url(_(r'^archive'), include('events.urls_archive', namespace='archive')),
start edit1
and link form main site to my app:
<a href="{% url 'archive:list' %}" title="{% trans 'Archive' %}">
{% trans 'Archive' %}
</a>
end edit1
start edit2
This is the url in my app urls_archive.py:
urlpatterns = [
url('^/(?P<page>\d+)/$', ArchiveListView.as_view(), name="list"),
url('^/(?P<slug>[-\w]+)/(?P<pk>\d+)$', ArchiveDetailView.as_view(), name="detail"),
]
end edit2
The code for my view:
class ArchiveListView(ListView):
model = Booking
queryset = Booking.objects.filter(period__type='archive').order_by('-date_start')
paginate_by = 80
page_kwarg = 'page'
Here is my template code:
{% if is_paginated %}
{% if page_obj.has_previous %}
<h4>Previous</h4>
{% endif %}
<span class="arrow header"><h4>Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}</h4></span>
{% if page_obj.has_next %}
<h4>Next</h4>
{% endif %}
{% endif %}
Please provide any help.
Thanks.
page_kwarg sets the key that the page number is passed in as. It doesn't affect how it is used in the page. For some reason, you are outputting that directly as /{{ page_obj.previous_page_number }}, which resolves to just /2. You should output it in the same format as it is passed in:
?page={{ page_obj.previous_page_number}}
Edit
If you want your page number to be specified as part of the path arguments, you should use the url tag, like any other URL:
{% url 'archive' page=page_obj.previous_page_number %}

How to add new created page url to modified django-oscar

I'm a beginner in django-oscar and I try to manage a new view on the page.
I've already created two pages with django-oscar dashboard,
https://ibb.co/cM9r0v
and made new buttons in the templates:
Lib/site-packages/oscar/templates/oscar/partials/nav_primary.html
https://gist.github.com/Kalinar/076fc8144869c3b50fc0bc9e52f825e4
I have no idea how to make a good a href="???" to new pages in buttons ... can someone help?
Maybe there is better way to do it, can you explain it to me?
Oscar uses Django's flatpages app, so you can use their template tags to dynamically add in navigation links to the pages you create.
{% load flatpages %}
{% get_flatpages as flatpages %}
{% for page in flatpages %}
<li class="dropdown active">
<a href="{{ page.url }}" class="dropdown-toggle" {% if not expand_dropdown %} data-toggle="dropdown"{% endif %}>
{% trans page.title %}
</a>
</li>
{% endfor %}
You can find more information on the flatpages app in the Django flatpage documentation.
You already know the title and of course the id of the page, and each page is an instance of the model PagePromotion, just query the model for such name/id and use the attribute page_url of the returned instance to send it within a variable in the context of the view that renders the menu.
Then in the template:
href="{{ variable_containing_url }}"

Django template multiple url return

I've 2 urls pointing to the same place:
url(r'^index/$', 'proj.views.index'),
url(r'^index/show_closed/$', 'proj.views.index'),
If I use in a template file this:
{% url proj.views.index %}
it' ll return /index/show_closed
and this is OK, but how can i make {% url %} to return only /index ?
Use named patterns; giving your pattern a unique name:
url(r'^index/$', 'proj.views.index', name='index_direct'),
url(r'^index/show_closed/$', 'proj.views.index', name='index_closed'),
then refer to those names in the {% url %} tag instead of the view:
{% url 'index_direct' %}
{% url 'index_closed' %}

Categories