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

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

Related

Django URLs Reverse working but not updating my URL

I'm new to Django 4.0.4.
I'm trying to use reverse in model to dynamically change the url without affecting other branch not affecting.
url.py:
urlpatterns = [
path('', home_view, name='home'),
path('products/', product_list, name='product_list'),
path('products/<int:myid>/', dynamic_lookup_view, name='product-detail'),
path('admin/', admin.site.urls),
]
models.py
def get_absolute_url(self):
return reverse("product-detail", kwargs={"myid": self.id})
html
<p>
{{instance.id}} {{instance.title}}
</p>
Output(working):
enter image description here
enter image description here
Problem:
when i change root url for dynamic_lookup_view from 'products/int:myid/' to 'ps/int:myid/' in url.py
path('products/', product_list, name='product_list'),
path('p/<int:myid>/', dynamic_lookup_view, name='product-detail'),
There is no update in my instance.get_absolute_url in my html!?

Modified urlpatterns in 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' %}

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

Django LOGIN_REDIRECT_URL failing when redirecting to the users profile

I'm using django-registration-redux and have most of it working. I'm trying to redirect to the user profile after login. Currently the URL for user profile is:
url(r'^user/(\w+)/$', views.profile, name='profile'),
...and the view for the profile is:
def profile(request, username):
user = get_object_or_404(User, username=username)
products = Product.objects.filter(user=user)
if not request.user == user:
return render(request, 'no.html')
else:
return render(request, 'profile.html', {'user':user,'products': products})
I've added LOGIN_REDIRECT_URL = 'profile' to settings.py but am getting the error:
Reverse for 'profile' with no arguments not found. 1 pattern(s) tried: ['user/(\\w+)/$']
I've gone around this so many times I'm totally confused. I could simply set LOGIN_REDIRECT_URL = 'home' and be done with it, but then I wouldn't have gotten past this error. Do I need to create a different view for this?
EDIT:
If I set LOGIN_REDIRECT_URL to 'home' or 'products' or any other URL it works - just not for 'profile'. Here's my urls.py:
urlpatterns = [
url(r'^$', views.HomePage.as_view(), name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^designers/', views.DesignersView.as_view(), name='designers'),
url(r'^subscribe/$', views.subscribe, name='subscribe'),
url(r'^products/$', views.products, name = 'products'),
url(r'^product/$', ProductListView.as_view(), name='product_list'),
url(r'^user/(\w+)/$', views.profile, name='profile'),
url(r'post_url/', views.post_product, name='post_product'),
url(r'^([0-9]+)/$', views.detail, name = 'detail'),
url(r'^like_product/$', views.like_product, name='like_product' ),
url(r'^profile/edit/$', views.edit_profile, name='edit_profile'),
url(r'^(?P<pk>\d+)/edit/$', PostUpdateView.as_view(), name='product-edit'),
url(r'^(?P<pk>\d+)/delete/$', PostDeleteView.as_view(), name='product-delete'),
]
I'm still searching - just not finding a solution, yet.
Finally found a way to do this. I created a login view in my views.py:
from django.contrib.auth.views import LoginView
class LoginView(LoginView):
def get_success_url(self):
return reverse('profile', args=[self.request.user.username])
Also added this to my urls.py to reflect the new view:
url(r'^accounts/login/$', LoginView.as_view(), name='login'),
Removed LOGIN_REDIRECT_URL from settings.py and it worked.
Your regex url isn't correct. Change:
url(r'^user/(\w+)/$', views.profile, name='profile'),
To
url(r'^user/(?P<username>[\w\-]+)/$', views.profile, name='profile'),

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