I'm giving Python / Django a ago, going alright so far. I'm in the middle of setting up Django authentication, but I've hit a error;
Reverse for 'user_review_list' not found. 'user_review_list' is not a valid view function or pattern name.
Here are my views:
def user_review_list(request, username=None):
if not username:
username = request.user.username
latest_review_list = Review.objects.filter(user_name=username).order_by('-pub_date')
context = {'latest_review_list':latest_review_list, 'username':username}
return render(request, 'reviews/user_review_list.html', context)
In my base.html I call the following:
<li><a href="{% url 'reviews:user_review_list' user.username %}">Hello {{ user.username }}</li>
I've checked my other html templates and they all seem to be calling it correctly, is there anything I'm missing?
EDIT: URL's
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^reviews/', include(('reviews.urls', 'reviews'), namespace='reviews')),
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
]
Apps URL's
from django.conf.urls import url
from . import views
app_name = 'reviews'
urlpatterns = [
# ex: /
url(r'^$', views.review_list, name='review_list'),
# ex: /product/5/
url(r'^review/(?P<review_id>[0-9]+)/$', views.review_detail, name='review_detail'),
# ex: /product/
url(r'^product$', views.product_list, name='product_list'),
# ex: /product/5/
url(r'^product/(?P<product_id>[0-9]+)/$', views.product_detail, name='product_detail'),
url(r'^product/(?P<product_id>[0-9]+)/add_review/$', views.add_review, name='add_review'),
]
Review.objects.filter() will return a list.
For a single user, you should use Review.objects.get() method
As #Exprator pointed out I was missing user_review_list from my app URL's.
Related
I am getting the above error when I try to access the landing page.
What am I missing?
Traceback
NoReverseMatch at /
Reverse for 'fleet' not found. 'fleet' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 2.2.6
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'fleet' not found. 'fleet' is not a valid view function or pattern name.
Here is the base.html code
<button>
` Fleet Admin
</button>
and below is the app urls.py file
from django.urls import path
from .admin import fleet_admin_site
app_name = 'trucks'
urlpatterns = [
path('fleet/', fleet_admin_site.urls, name="fleet"),
]
and the main urls.py file
from django.contrib import admin
from django.urls import path, include, reverse
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', include('workers.urls')),
path('admin/', include('trucks.urls')),
path('', TemplateView.as_view(template_name='base.html')),
]
admin.py file where I extend the AdminSite
class FleetAdminSite(admin.AdminSite):
site_header = ''
site_title = ''
index_title = ''
fleet_admin_site = FleetAdminSite(name='fleet_admin')
by seeing your code
you need to add method or class not any extension
path('fleet/', fleet_admin_site.urls, name="fleet"),
path(route, view, kwargs=None, name=None)
refer this
You are including the fleet admin with:
urlpatterns = [
path('fleet/', fleet_admin_site.urls, name="fleet"),
]
You can't do {% url 'trucks:fleet' %} to reverse fleet_admin_site.urls. You need to reverse a particular admin URL.
For example, to reverse the index, you would do:
{% 'trucks:fleet_admin:index' %}
In the above, you use trucks because you have app_name = 'trucks' in the urls.py, fleet_admin because that is the namespace in fleet_admin_site = FleetAdminSite(name='fleet_admin'), and index because that's the view you want to reverse.
Finally, the name in your path() doesn't have any effect, so I would remove it.
urlpatterns = [
path('fleet/', fleet_admin_site.urls),
]
register.html is not accessible on 'register/' url
tried changing url to '' and webpage is accessible but not on 'register/'
projects urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('login/',include('login.urls')),
path('admin/', admin.site.urls),
]
apps urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.indexView, name = "home"),
path('dashboard/', views.dashboardView, name="dashboard"),
#path('login/',),
path('register/',views.registerView, name="register_url"),
#path('logout/',),
]
views.py
from django.shortcuts import render
def indexView(request):
return render(request,'index.html')
def dashboardView(request):
return render(request,'dashboard.html')
def registerView(request):
return render(request,'register.html')
templates/register.html
Either you type login/ before evry URL pattern of apps urls.py file while testing URL.
or
You should include url mapping of apps urls.py in main urls.py
This might help you.
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('',include('apps.urls')), #put you app name here
path('admin/', admin.site.urls),
]
apps urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.indexView, name = "home"),
path('dashboard/', views.dashboardView, name="dashboard"),
path('register/',views.registerView, name="register_url"),
]
This will work and you can add login/ url mapping inside the apps urls.py.
This type of flow will make it easy to understand .
Use url_name for URL calling:
{% url 'register_url' %}
This will directly search for 'register_url' in the project and will return the path.
url_name must be unique so make sure your url_name is uniquely defined.
I am having a trouble mapping urls correctly. I've included my code below.
I am able to run the code just fine, but when I click the "about" hyperlink, I get an error saying
The current URL, rango/about/, didn't match any of these.
When I put in the URL just "rango/", removing the "about", I get the following error:
The current URL, rango/, didn't match any of these.
I am a complete beginner with Django and have been going through the Tango with Django book, but currently stuck with the exercises on Ch3.
Any help is much appreciated. Thank you!
tango_with_django_project.urls.py
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from rango import views, urls
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^rango/',include('rango.urls')),
# above maps any URLs starting with rango/ to
# be handled by the rango application
url(r'^admin/', admin.site.urls),
]
rango.urls.py
from django.conf.urls import url
from rango import views
urlpatterns = [
url(r'^rango/', views.index, name='index'),
url(r'$^rango/about/',views.about,name='about'),
]
rango.views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Rango says hey there partner! \
<br/> <a href='/rango/about/'>about</a>")
def about(request):
return HttpResponse("Rango says here is the about page. \
<br/> <a href='/rango/'>index</a>")
# rango.urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^about/$',views.about,name='about'),
]
I am trying to process a form in django/python using the following code.
home.html:
<form action="{% url 'home:submit' %}" method='post'>
views.py:
def submit(request):
a = request.POST(['initial'])
return render(request, 'home/home.html', {
'error_message': "returned"
})
urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^submit/$', views.submit, name='submit')
]
when I try to run it in a browser I get the error:
NoReverseMatch at /home/ u'home' is not a registered namespace
and another error message indicating a problem with the form.
You should just change you action url in your template:
<form action="{% url 'submit' %} "method='post'>
On the note of url namespaces...
In order to be able to call urls using home namespace you should have in your main urls.py file line something like:
for django 1.x:
url(r'^', include('home.urls', namespace='home')),
for django 2.x and 3.x
path('', include(('home.urls', 'home'), namespace='home'))
In your main project, open url.py first. Then check, there should be app_name declared at first. If it is not, declare it.
For example, my app name is user info which is declared in url.py
app_name = "userinfo"
urlpatterns = [
url(r'home/', views.home, name='home'),
url(r'register/', views.registration, name='register')
]
I also faced the same issue.
it is fixed now by adding
app_name = "<name of your app>"
in app/urls.py
For Django 3.0, if you're handling your urls within the app and using include with path, in your project/urls.py:
urlpatterns = [
path(os.getenv('ADMIN_PATH'), admin.site.urls),
path('', include('my_simple_blog.urls', namespace='my_simple_blog')),
path('account/', include('account.urls', namespace='account')),
]
You need to specify namespace in include.
And then in your app/urls.py:
app_name = 'account'
urlpatterns = [
path('register/', registration_view, name='register'),
path('logout/', logout_view, name='logout'),
path('login/', login_view, name='login'),
]
The app_name must match the namespace you've specified in project/urls.py.
Whenever you're referring to these urls, you need to do it like this:
{% url 'namespace:name' %}
If you're using it with redirect:
return redirect('namespace:name')
For the namespace error,
Make sure you have linked the app's url in the main urls.py file
path('app_name/',include('app_name.urls'))
also in the urls.py of your app,make sure you mention the app's name as
app_name='app_name'
Also make sure you have registered the app's name on your installed apps in settings.py
As azmirfakkri has said if you're using redirect, dont use this {% url 'namespace:name' %} syntax, use return redirect('namespace:name').
Probably 2 things could be a root cause,
in app/urls.py do include as below
app_name = 'required_name'
and in project urls.py also include the app_name
url(r'^required_name/$',views.home,name='required_name'),
Check: register app in settings.py INSTALLED_APPS
tag name must be unique in the urls.py file inside your application package inside the project! it is important for the template tagging to route whats what and where.
now [1] inside the urls.py file you need to declare the variable appName and give it the unique value. for example appName = "myApp"; in your case myHomeApp and [2] also define the urlpatterns list...
urlpatterns = [..., url(r'^submit/$', views.submit, name='submit'), ...];
in the html file just change the url tag to:
<form action="{% url 'myHomeApp:submit' %}" method='post'>
this should sifuce... else just write here and we'll see how to continue on
A common mistake that I always find is when you have some name space in your template,
and in YourApp.url you don't have any name space so if you should use name space add
in YourApp.url something like this
app_name = "blog"
then on your temples make sure you add your name space,
so you will have some thing like this "assumption errors are coming from edit.html"
then on that particular template you will do this
"{% url 'blog:vote' pk=post.pk %}" "{% url 'blog:post_category' category.name %}"
if you happen to be nesting include(s, the namespace compounds, eg. topappname:appname:viewname
Maybe someone will find this suggestion helpful.
Go to your applications urls.py and type this before the urlpatterns:
app_name = 'Your app name'
I got the same error below:
NoReverseMatch at /account/register/ 'account' is not a registered
namespace
So, I set "app_name" with the application name "account" to "account/urls.py" as shown below then the error above is solved:
# "account/urls.py"
from django.urls import path
from . import views
app_name = "account" # Here
urlpatterns = [
path("register/", views.register_request, name="register")
]
Check your urls.py
urlpatterns = [
re_path(r'^submit/expense/$', views.submit_expense, name='submit_expense'),
re_path(r'^submit/income/$', views.submit_income, name='submit_income'),
re_path(r'^register/$', views.register, name='register'),
]
then open template.html
put for example register register in your HTML tag like this:
<a class="navbar-brand" href="{% url 'register' %}">
For anyone who struggled on this error like me: After reading the solutions to this question, I was setting namespace in include function in a wrong urls file. Make sure you are modifying the right urls file. For me it was putting it in the main url.py besides settings.py. I hope this answer helps anyone who was confused as I was.
This worked for me:
In urls.py
urlpatterns = [
path("admin/", admin.site.urls),
path("", views.index),
path("test/", views.test, name = 'test')]
In views.py:
def test(request):
return render(request, "base/test.html")
In the template:
href="{% url 'test' %}"
I am new in Python, I have a problem with the admin URL.
When I type
localhost:8000/admin/
My browser tells me:
DoesNotExist at /admin/
Question matching query does not exist
My index page and detail pages work fine.
I thought it was a mistake in the order of URL's,
I changed the order of the admin and detail URL,
but still nothing.
Can somebody please give me a hint.
project/urls.py
from django.conf.urls import include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
urlpatterns = [
url(r'^$', 'books.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<book_title>[\w_-]+)/$', 'books.views.detail', name='detail'),
]
urlpatterns += staticfiles_urlpatterns()
books/views.py
from django.shortcuts import render
from .models import Question
def index(request):
latest_question_list = Question.objects.all
context = {'latest_question_list': latest_question_list}
return render(request, 'books/index.html', context)
def detail(request, book_title):
question = Question.objects.get(title=book_title)
return render(request, 'books/detail.html', {'question': question})
I forgot to put parentheses for Question.objects.all So silly of me.
add this before urlpatterns in urls.py
admin.autodiscover()