Modified urlpatterns in django - django

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' %}

Related

A issue to use URL template tag in django

I am currently learning Django. When I apply a url template tag, I found that the output of the url tag is not what I expected. I have read the Django Documents, but it does not help.
{{ movie.title }}
from django.urls import path
from . import views
app_name = 'movies'
urlpatterns = [
path('', views.index, name='index'),
path('<int:movie_id>', views.detail, name='detail')
]
The output of the url tag is localhost/movies/%7B%%20url%20'movies:detail'%20movie.id%20%
which is not as I expected: localhost:8000/movies/1
Modify lines as follow:
{{ movie.title }}
...
path('movies/<int:movie_id>/', views.detail, name='detail')

How to call a template?

How can I call the the about.html from the following index.html? What to add to urls and views?
All the html files including about and index are collected in the project/static folder.
# This is part of the index.html, where I want to call the about.html
<div class="card-footer">
Learn More
</div>
# Here is the project/urls.py
from django.contrib import admin
from django.urls import path
from app1 import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
]
urlpatterns += staticfiles_urlpatterns()
If you are in one html file and want to open another html file you can put something like this in your html file:
Go to About
This will create a clickable link on the words Go to About. You can then position this anywhere you want on your page. In your urlpatterns you would need to make a new path like this
path('about', view.AboutView.as_view(), name='about')
Within your views.py you have to make a class called AboutView using templateView and put the name of your html file as template_name like this:
from django.views.generic.base import *
class AboutView(TemplateView):
template_name = "about.html"
Let me know if you need more help understanding this code or want to do something else
add about url in your urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('about/', views.about, name="about"),
]
add about views in your views.py file
def about(request):
# render your template here
in index.html
<div class="card-footer">
Learn More
</div>

Why NoReverseMatch at occurred on django 2.0

Error says :
Reverse for 'list' not found. 'list' is not a valid view function or pattern name.
My code are on below.
html template where error is in:
{% block more_posts %}<button type="button" href="{% url 'website:list' %}">more posts</button>{% endblock %}
my_project/urls.py:
urlpatterns = [
re_path(r'^admin/', admin.site.urls),
re_path(r'^', include('website.urls')),
]
website/urls.py:
app_name = 'website'
urlpatterns = [
re_path(r'^about/$', TemplateView.as_view(template_name='website/about.html'), name='list'),
re_path(r'^$', views.main, name='main'),
]
Is there are something wrong with my url namespace settings?
add namespace
re_path(r'^', include('website.urls',namespcae="website")),

NoReverseMatch: Reverse for 'detail' not found. (Django Tutorials)

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).

Django flaky url (randomly omits WSGIScriptAlias)

My Django site is producing URLs that intermittently omit my WSGIScriptAlias. If I simply print out {% url 'index' %} in my index.html (see my urls.py settings below) I randomly (around 50% of the time) get either:
MySiteAlias/MySite
which is correct, or
MySite/
which is incorrect.
myapp/urls.py:
from django.conf.urls import url,include
urlpatterns = [
url(r'^MySite/', include('mysite.urls')),
]
mysite/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
and views.index basically does return render(request, 'mysite/index.html). Any ideas on how to fix this?
I'd imagine you might have two urls with the same name, this is where namespaces will help. If you provide a namespace for your mysite.urls then there is no confusion on where you should go to
url(r'^MySite/', include('mysite.urls', namespace='mysite')),
{% url 'mysite:index' %}