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
Related
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"),
]
So, I am not really lucky with latest django version tutorials, so I've had some problems with things that changed between some versions. One of this things is: althought I do exactly as I read/watch in the tutorials I always get the same result - all urls redirect to the same HTML page.
Here is my root urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('theblog.urls')),
]
Here is my app urls:
from django.conf.urls import url, include
from .views import HomeView, ArticleDetailView
urlpatterns = [
url('', HomeView.as_view(), name='home'),
url('^article/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),
]
For example, when I go to localhost:8000/articles/1 (or any other pk), it renders home.html (HomeView class) as if it was localhost:8000/.
Hope you can help me. Thanks!
There are two things wrong with your code.
url('^article/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),
This will not work. If you want to use url. you can't use <int:pk>, you need to use a RegEx:
url("article/([0-9]+)/", ArticleDetailView.as_view(), name="article-detail")
Note that this will be deprecated in the future and if you are using django >=2.0 you should use path:
path("article/<int:article>/", ArticleDetailView.as_view(), name="article-detail")
However this will still direct you to the wrong view. django stops after the first URL pattern match.
Switch them around to fix that:
urlpatterns = [
url("article/([0-9]+)/", ArticleDetailView.as_view(), name="article-detail"),
# path("article/<int:article>/", ArticleDetailView.as_view(), name="article-detail"), # alternative with path instead of url
url('', HomeView.as_view(), name='home')
]
It may be due to this line
url('', HomeView.as_view(), name='home'),
Because url wraps re_path there may be some logic which will treat the blank regex string as a wildcard. Try changing it to '/'
url('/', HomeView.as_view(), name='home'),
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
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')),
]
I'm new to Django and I currently have two problems that I can't figure out from reading online:
URLs... I have an app 'cq' and the project is 'mysite' here is what I have in mysite's urls.py
urlpatterns = [
url(r'^cq/', include('cq.urls')),
url(r'^admin/', include(admin.site.urls)),
]
and this is what I have in cq's urls.py
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<team_id>[0-9]+)/$', views.team, name='team'),
]
However I don't want do to cq/team_id. I just want to be able to go directly to /team_id. Is there any way to do it?
I want an entry in the admin view of the database to look very custom, i.e. I want to write my own html.. How do I do it?
Thanks!
When you define
url(r'^cq/', include('cq.urls')),
it means all urls in cq.urls will have to start with cq/. For you case, just do
urlpatterns = [
url(r'^cq/', include('cq.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^(?P<team_id>[0-9]+)$', views.team, name='team') #without trailing slash for /team_id, but with for /team_id/
]
Of course, you don't need this rule anymore in cq urls.py