I'm making my studying project, called GuitarStore. I have two application in this project - shop and blog. Here is fictional situation: I have that "contacts" page for my team of authors, and another "contacts" page for my sale team.
Here's piece ofmy guitarstore/urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
path('', RedirectView.as_view(url='/shop/', permanent=True)),
]
piece of shop/urls.py:
path('', views.shop, name='shop'),
re_path(r'^section/(?P<id>\d+)$', views.section, name='section'),
re_path(r'^product/(?P<pk>\d+)$', views.ProductDetailView.as_view(), name='product'),
re_path(r'^manufacturer/(?P<id>\d+)$', views.manufacturer, name='manufacturer'),
path('delivery', views.delivery, name='delivery'),
path('contacts', views.contacts, name='contacts'),
path('search', views.search, name='search'),
and piece of blog/urls.py:
path('', views.blog, name='blog'),
re_path(r'^blog/(?P<month_n_year>\w{7})$', views.blog_filtered, name='blog'),
re_path(r'^post/(?P<pk>\d+)$', views.PostDetailView.as_view(), name='post'),
path('gallery', views.gallery, name='gallery'),
path('about', views.about, name='about'),
path('contacts', views.contacts, name='contacts'),
path('search', views.search, name='search'),
I thought Django would come up with two separate paths like "shop/contacts" and "blog/contacts" but not. I have that piece of html template:
<div class="top-nav">
<ul class="cl-effect-1">
<li>Main</li>
<li>Abour us</li>
<li>Blog</li>
<li>Contacts</li>
</ul>
</div>
and all I get is "shop/contacts" for the last part of menu. Should I make sure that all my urls have a bit of excess so I can't overlap them or I just have to make some settings so my applications have some priorities for their own paths?
For now I've tried naming them in different way even though I think it's not how it should be.
For example, I hade "index.html" template file in the template folder of shop application and another one template file called "index.html" in template folder of blog application. My project have gone wild so I renamed them for "shop.html" and "blog.html" even though I want to keep them called the old way.
I found out that I can use {% url 'blog:contacts' %} instead of {% url 'contacts' %} so Django does not get confused about which one url should it show and use. But even though my blog/views.py consists
def contacts(request):
return render(request, 'bl_contacts.html')
(I renamed "contacts.html" to "bl_contacts.html"), my blog/urls.py consists:
app_name = 'blog'
urlpatterns = [
####....
path('contacts', views.contacts, name='contacts'),
and my template/bl_contacts.html now consists:
<div class="top-nav">
<ul class="cl-effect-1">
<li>Main</li>
<li>About us</li>
<li>Blog</li>
<li>Contacts</li>
</ul>
</div>
I still got my shop/contacts page, even though my browser shows blog/contacts path.
Related
I have an issue with my navigation menu items. When the user is in the page of Item X and they click over to the Item Y of the navigation menu, they should be taken to example.com/ItemY. Instead, they are taken to example.com/ItemX/ItemY which results in a page-not-found error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/biology/ItemX/ItemY
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
chemistry/<slug:slug>/ [name='chemi']
summernote/
blog/ <str:parent>/<slug:slug>/ [name='blog_list']
^media/(?P<path>.*)$
The current path, blog/biology/ItemX/ItemY, didn’t match any of these.
These are my project url patterns:
#urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('chemistry.urls')),
path('summernote/', include('django_summernote.urls')),
path('blog/', include('blog.urls')),
]
And these are my app urls:
#blog/urls.py
urlpatterns = [
path('<str:parent>/<slug:slug>/', views.BlogDetail.as_view(), name='blog_list'),
]
And here is the HTML template that contains the navbar:
<div class="card-body">
<nav><ul>
{% for blog in blog_list %}
<li>{{blog.title}}</li>
{% endfor %}
</ul></nav>
</div>
How can I fix this?
To solve this I changed the <a> tags to this:
<li><a class='text-muted' href="{{ blog.get_absolute_url }}">{{blog.title}}</a></li>
And I added a get_absolute_url method to the Blog model:
def get_absolute_url(self):
return reverse("blog_list", args=[str(self.parent), str(self.slug)])
I am hoping to better understand the urlpatterns in Django as well as figuring out this puzzle:
To turn the current page's link on the navbar to be active (ie, highlighted, as in class="nav-item active", I use {% if 'somelink' in request.path %}, which mostly works.
But {% if '/' in request.path %} doesn't work for the homepage. The "Home" link remains highlighted whether or not one is on the page. Is it because all paths must go through path('/' ...)? How does one isolate the root url then?
urlpatterns = [
path('', views.index, name='index'),
...
]
I modified url in django, i can use tag {% url 'student-register' %} in template but when i call {% url 'student-reg' %} it return error reverse-not found, anyone can describe what happened ? this is my code :
from django.urls import path, include
from .views.home import *
from .views.teacher import *
urlpatterns = [
path('', ViewHomePage.as_view(), name='home'),
path('logout', LogoutAccount.as_view(), name='logout'),
path('teacher/', include(([
path('', ViewTeacherHome.as_view(), name='teacher'),
path('student-register', ViewStudentRegister.as_view(), name='student-register'),
], 'exam'), namespace='teacher')),
path('student/', include(([
path('', ViewTeacherHome.as_view(), name='student'),
path('student-reg', ViewStudentRegister.as_view(), name='student-reg'),
], 'exam'), namespace='student'))
]
Since you specify student and exam namespaces for nested urls list you should use:
{% url 'student:exam:student-reg' %}
I've been going through the Django 2 tutorials.
I got the following error:
#Error:
#django.urls.exceptions.NoReverseMatch
#django.urls.exceptions.NoReverseMatch: Reverse for 'detail' not found. 'detail' is not a valid view function or pattern #name.
Did some googling and confirmed that I had named my view 'detail' and also had my app named.
Below are my codes.
Please tell what is wrong. I am following the tutorial by heart, but this came up. How can I fix it keeping in par with the tutorials? Thank you!
Files:
mysite/polls/templates/polls/index.html
{% for question in latest_question_list %}
<li>{{ question.question_text }}</li>
{% endfor %}
mysite/polls/urls.py
app_name = 'polls'
urlpatterns = [
path('', views.index, name='index'),
# ex: /polls/
# path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
# ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
path('<int:question_id>/vote/', views.vote, name='vote'),
]
mysite/polls/views.py
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context)
Additional:
mysite/urls.py
urlpatterns = [
path('polls/', include('polls.urls', namespace='polls')),
path('admin/', admin.site.urls),
]
You haven't defined any function named as 'detail' in views.py file.
Add this code.
def detail(request, id):
context = dict()
return render(request, 'polls/index.html', context)
You have to add results and vote function as well.
Remove the commented lines from your index.html file. Syntax in these lines is not right and Django tries to parse commented lines as well before rendering.
Remove from mysite/urls.py the namespace as you already specified app's app_name
or you can just remove the app_name and keep the namespace (not sure if this works in Django 2.0 as there are some tweaks in app_name and namespace in this version).
I'm looking for add a new reverse url in order to redirect to /admin/auth/user/ in my Django Admin page.
In my template, I already have : href="{% url "admin:index" %}" line which let to overcome to the admin page.
I would like to go directly to the users manage page and groups manage page respectively admin/auth/user/ and admin/auth/group.
My urls.py file looks like :
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name=os.path.join(settings.BASE_DIR, 'Accueil/templates/Choice.html')),
name='choice'),
url(r'^admin/', admin.site.urls),
url(r'^Identity/', include('Identity.urls')),
url(r'^Accueil/', include('Accueil.urls')),
url(r'^Home/', include('log.urls')),
url(r'^Informations/', include('Informations.urls')),
url(r'^Configurations/', include('Configurations.urls')),
url(r'^__debug__/', include(debug_toolbar.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
How I can write href="{% url "admin:index" %}" in order to add user and group ? Up to now, I don't find a way to do that.
Thank you by advance
You can reverse the user and group changelist urls with:
{% url "admin:auth_user_changelist" %}
{% url "admin:auth_group_changelist" %}
See the docs on reversing admin urls for more info.