NoReverseMatch found in django for app homepage - python

I am trying to redirect an link from app to another page view function. But after pointing the page correctly I am getting NoReverseMatch Found error on apps main page which haves no connection to it.
This is urls.py of main Project
urls.py
urlpatterns = [
path('', views.home, name="home"),
path('admin/', admin.site.urls),
path('teacher/', include('teacher.urls')),
path('student/', include('student.urls')),
]
This is urls.py for respective app which is teacher
urls.py
urlpatterns = [
path('', views.index, name="index"),
path(r'^detailed/(?P<reportid>\d{0,4})/$', views.detailed, name="detailed"),
]
I am also including views.py as error is pointing at view.py
views.py
def index(request):
return render(request, 'teacher/report.html')
def detailed(request, reportid):
weeklyr = wreport.objects.all()
dailyr = dreport.objects.all()
split = SplitOfWeek.objects.all()
return render(request, 'teacher/detailed.html')
I have tried adding r'^teacher/$' at main urls.py and r'^$' at urls.py of teacher app but after adding it shows there is url found for teacher.
This is the detailed error message:
Reverse for 'detailed' with no arguments not found. 1 pattern(s) tried: ['teacher/\\^detailed/\\(\\?P(?P<reportid>[^/]+)\\\\d\\{0,4\\}\\)/\\$$']

You shouldn't use regexes with path
A simple fix would be to do:
urlpatterns = [
path('', views.index, name="index"),
path('detailed/<int:reportid>/', views.detailed, name="detailed"),
]
However this would allow any number for reportid. If you really to limit the length to four characters, then you could use the regex with re_path:
urlpatterns = [
path('', views.index, name="index"),
re_path(r'^detailed/(?P<reportid>\d{1,4})/$', views.detailed, name="detailed"),
]
Another option would be to register a custom path converter for reportid.

After help in understanding issue from #Alasdair i found that adding a default value for reportid in view function is solution.
so views.py changed to this
def detailed(request, reportid=None):
Also to go to that default value I added url for default view.
So urls.py changed to this
urlpatterns = [
path('', views.index, name="index"),
path('detailed/<int:reportid>/', views.detailed, name="detailed"),
path('detailed/', views.detailed, name="detailed"),
]

Related

How to add custom django admin site and urls without the prefix in every pattern?

I am trying to include the urls of my custom admin site of one of my apps in a Django project. I would like the path to be foo-admin/.... I managed to do so by including this prefix in all urlpatterns in the app's urls.py. However if I try to add the prefix to the project's urls.py and have only the suffixes in the app's urls, it breaks. It's something like this:
This works:
Project's urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('my_app.urls')),
]
my_app/urls.py:
urlpatterns = [
path('foo-admin/', my_app_admin_site.urls),
path('foo-admin/foo/', views.some_view),
path('foo-admin/foo/<slug>/', views.MyGenericView.as_view()),
]
This doesn't work:
Project's urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('foo-admin/', include('my_app.urls')),
]
my_app/urls.py
urlpatterns = [
path('', my_app_admin_site.urls),
path('foo/', views.some_view),
path('foo/bar/<slug>/', views.MyGenericView.as_view()),
]
The error I'm encountering is:
NoReverseMatch at /foo-admin/
Reverse for 'app_list' with keyword arguments '{'app_label': 'my_app'}' not found. 1 pattern(s) tried: ['admin/(?P<app_label>auth|app1|app2|django_mfa|axes)/$']

django The current path,didn't match any of these

i want to try another apps in django but i got problem when access another apps.
Page not found
tree:
main:
search:
index.html
scrape.html
preprocessing:
index.html
the scenario like this. from scrape.html i want to access index.html in preprocessing but got an error path not found. I've done to add apps in settings.py
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index,),
path('scrape/',views.scrape,),
]
slice of scrape.html:
Preprocessing
preprocessing url.py
path('', views.index),
let me know where did I go wrong, thx for your help
Your anchor tag is written as:
<a href = "/preprocessing" ...>
The problem here is that your url pattern is like 'preprocessing/', notice that it ends in a trailing slash while your anchors url doesn't. Furthermore in your settings you set APPEND_SLASH = False.
Normally when Django finds a url not ending in a trailing slash it appends a slash to it and redirects the user to this new url. But you have stopped this behaviour by setting APPEND_SLASH = False. As the first step I would advice you to change this back to APPEND_SLASH = True.
Next you should always name your urls and use those names to refer to the urls. So your urlpatterns should be:
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index, name='index'),
path('scrape/',views.scrape, name='scrape'),
]
preprocessing url.py
path('', views.index, name='preprocessing'),
Now in your templates you would simply use the url template tag:
Preprocessing

NoReverseMatch at /Reverse for 'newsdesc' with keyword arguments '{'slug': ''}' not found

``I am running a django project to display a website. Here is my views.py:
def news_desc(request,slug):
# request.session.flush()
news=NewsPort.objects.get(news_title_slug=str(slug))
return render(request,'accounts/newsdesc.html',{'news':news, 'slug':slug})
urls.py
urlpatterns = [
path('signup/', views.signup, name='signup'),
path('login/', views.user_login, name='user_login'),
path('logout/', views.user_logout, name='user_logout'),
# path('', views.index, name='index'),
path('<int:pk>/',views.index_with_pk, name='index_with_pk'),
path('profile/<int:pk>/',views.profile_detail,name='profile_detail'),
path('profile/<int:pk>/edit/',views.profile_edit,name='profile_edit'),
path('profile/<int:pk>/wallet/',views.wallet_view,name='wallet_view'),
path('profile/<int:pk>/wallet/transac',views.history_transac,name='history_transac'),
path('news/all/', views.news_all, name='news_all'),
path('profile/<int:pk>/maps/',views.map_view,name='map_view'),
path('news/<slug:slug>/',views.news_desc,name='newsdesc'),
]
When I run, I'm getting an error:
NoReverseMatch at /
Reverse for 'newsdesc' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['accounts/news/(?P<slug>[-a-zA-Z0-9_]+)/$']
The error is during template rendering.I cant able to login ,signup and logout.
Here is my main urls.py :
router=routers.DefaultRouter()
router.register('profiles',views.ProfileView)
router.register('requests',views.RequestsView)
router.register('transactions',views.TransactionView)
router.register('login',views.LoginView)
router.register('wallets',views.WalletView)
router.register('purchasedtickets',views.PurchasedTicketView)
router.register('grocerycontents',views.PurchasedContentView)
router.register('merchants',views.MerchantView)
router.register('merchantitems',views.MerchantItemView)
router.register('groceries',views.GroceryView)
router.register('commodityitems',views.CommodityItemView)
router.register('transview',views.TransactionsView,base_name='Transactions_view')
router.register('tokenidview',views.TokenIDView,base_name='Token_view')
router.register('barcodetransfer',views.BarCodeView)
router.register('apptransfer',views.AppTransferView)
admin.autodiscover()
urlpatterns = [
path('admin/', admin.site.urls),
path('home/',include(router.urls)),
path('', views.index, name='index'),
path('accounts/',include('accounts.urls'),name='accounts'),
path('merchants/',include('merchants.urls'),name='merchants'),
path('groceries/', include('groceries.urls'), name='groceries'),
path('home/rest-auth/', include('rest_auth.urls')),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Any help would be appreciated.
The problem is in the template that's used by views.index. It's likely how you're iterating over the NewsPort collection and generating the link to newsdesc. Or you have data that has an empty string for the slug. In which case you're either not setting it or it's a testing artifact.
A normal detail view will be as follows
def news_desc(request,slug):
news = get_object_or_404(NewsPort, slug=slug)
return render(request,'accounts/newsdesc.html',{'news':news})
Then you can give link of this detail view url in your list view as follows
Detail View

Django url patterns, redirect from root to other url

This should be an easy enough problem to solve for you guys:
I just started working with Django, and I'm doing some routing. This is my urls.py in the root of the project:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dashboard.urls')),
]
This is the routing in my dashboard app:
urlpatterns = [
path('dashboard', views.index, name='index'),
path('', views.index, name='index'),
]
Now let's say I want my users to be redirected to /dashboard if they go to the root of the website. So I would use '' as a route in the urls.py in the root, and then have everyone sent to /dashboard from the urls.py in the dashboard app. But when I do this I get the following warning:
?: (urls.W002) Your URL pattern '/dashboard' [name='index'] has a route beginning with a '/'. Remove this slash as it is unnecessary. If this pattern is targeted in an include(), ensure the include() pattern has a trailing '/'.
So I tried to use '/' instead of '', but since a trailing / is automatically removed from an url, the url wouldn't match the pattern. Should I ignore/mute this warning or is there another way to go about it?
This is the code that worked perfectly but gave me a warning earlier:
urlpatterns = [
path('/dashboard', views.index, name='index'),
path('', views.index, name='index'),
]
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dashboard.urls'))
]
You can use RedirectView to redirect from / to /dashboard/. Then use 'dashboard' when including the dashboard urls.
urlpatterns = [
path('admin/', admin.site.urls),
path('', RedirectView.as_view(pattern_name='dashboard:index')
path('dashboard/', include('dashboard.urls')),
]
You can then remove 'dashboard' from the path in dashboard/urls.py, as it is already in the include().
app_name = 'dashboard'
urlpatterns = [
path('', views.index, name='index'),
]
I've added app_name='dashboard' to match the namespace used above in pattern_name='dashboard:index'.
Note that Django projects usually use URLs with a trailing slash, e.g. /dashboard/ instead of dashboard.
If you really want to use URLs like /dashboard without a trailing slash, then the include should be
path('dashboard', include('dashboard.urls')),
If you do this, I suggest you set APPEND_SLASH to False in your settings.
You can try something like this:
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import RedirectView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^contacts/', include('appname.contacts.urls')),
url(r'^comments/', include('appname.urls')),
url(r'^subscriptions/', include('appname.partner.urls')),
url(r'^', RedirectView.as_view(url="/admin/"))
]
This is what I've done in my project so whenever the user go to 127.0.0.1:8000 it redirects to /admin

Django adding namespace to urls gives error Reverse for 'login' not found. 'login' is not a valid view function or pattern name

Im trying to add this url to my app's urlpatterns (i.e. MyProject/MyApp/urls.py):
url(r'^login/$', auth_views.LoginView.as_view(), name='login')
I have this snippet in one of my templates:
Login
Normally, clicking on the link takes you to the login page successfully. However, when I try to add a namespace to my urls (app_name = my_namespace) and change the reverse to
Login
it fails when I click on the link and I get the error
Reverse for 'login' not found. 'login' is not a valid view function or
pattern name.
While all the other urls I reverse work with the namespace, it is just the login reverse that fails. Any idea why?
Edit:
MyProject/MyProject/urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^clubinfo/', include('ClubInfo.urls')),
]
MyProject/MyApp/urls.py:
app_name = 'clubinfo'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^register/$', views.register, name='register'),
url(r'^login/$', auth_views.LoginView.as_view(), name='login'),
]
A snippet of the template:
Home
Login
Register
I can click on Home and Register, not login
Edit 2: auth_views is from this import:
from django.contrib.auth import views as auth_views
I think this may have something to do with why the program is raising an error.
It turns out the problem was in my login.html file which Django renders in its LoginView. I didn't use the namespace in one of my reverses in that file.
in your project urls do this:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^clubinfo/', include('ClubInfo.urls', namespace='clubinfo')),
]
now in clubinfo urls:
remove
app_name = 'clubinfo'
run de server again and try it should work that my way of doing
I find that your
app_name='clubinfo'
, but your urlpatterns is
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^clubinfo/', include('**ClubInfo**.urls')),
]

Categories