I have an issue where I would like my address (whether local or live) to point to my projects app as my home page, yet every change I make breaks the other URLs. My main site URL patterns are:
urlpatterns = [
path('admin/', admin.site.urls),
path("projects/", include("projects.urls")),
path("blog/", include("blog.urls")),
]
I have tried to change the path("project/", include("projects.urls")) to path("", include("projects.urls")) which breaks the blog index.
my blog has the following pattern:
urlpatterns = [
path("", views.blog_index, name="blog_index"),
path("<slug:slug>/", views.blog_detail, name="blog_detail"),
path("<category>/", views.blog_category, name="blog_category"),
]
And my projects:
urlpatterns = [
path("", views.project_index, name="project_index"),
path("<slug:slug>/", views.project_detail, name="project_detail"),
path("project/<tag>/", views.project_tag, name="project_tag"),
]
The conflict is coming from django not knowing the difference between:
http://example.com/blog
and
http://example.com/(project_slug_called_blog)
because they look functionally identical, just being a string.
You could reverse the order of the URLs so that blog is looked for first
urlpatterns = [
path('admin/', admin.site.urls),
path("blog/", include("blog.urls")),
path("", include("projects.urls")),
]
And that should work so long as not projects are called 'blog'
Or, failing that, you can make the project detail view's URL more explicit
path("<slug:slug>/view-details", views.project_detail, name="project_detail"),
I'd personally go with the second option as it makes the URL more human readable and is likely a more robust solution.
Related
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
I love DRF but I still find its documentation really frustrating, for real, a lot of advanced tips and examples are there but I still always dive into toughness and frustration when I'm looking for the most basic stuff of any topic of the amazing framework.
In this case, this resource
looks really cool! But they don't even start by how to make it work (or what could not be properly configured if it isn't working as in my case). Could someone tell me what's the holy api root URL or how to include/configure it? Because I don't see it in my URLs and so far I didn't find anything in the official docs.
I also followed the quickstart tutorial.
This is what I have:
My main urls.py:
api_urls = [
path('rest_framework/', include('rest_framework.urls', namespace='rest_framework')),
path('', include('project.posts.urls')),
path('', include('project.users.urls')),
]
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include(api_urls)),
]
users.urls:
router = SimpleRouter()
router.register(r'users', UserViewSet, basename='users')
urlpatterns = [
path('', include(router.urls)),
]
posts.urls:
router = SimpleRouter()
router.register(r'posts', PostViewSet, basename='posts')
urlpatterns = [
path('', include(router.urls)),
]
I can log in into the DRF panel but then I get a redirection error.
Also I can see the pages if I manually go to a valid endpoint URL, but it's not an actual navigation from the root point of the API.
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'),
I've encountered a one nasty bug in my code while developing a personal blog in Django. Basically, I've changed my urls.py file by adding a couple of rules to make certain views accessible.
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from blog import views
urlpatterns = [
# Examples:
# url(r'^$', 'blogas.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),
url(r'^about/$', views.about, name='about'),
url(r'^posts/$', views.posts, name='posts'),
]
Everything seems to be working except when I try access http://127.0.0.1:8000/about or /posts, Django throws out a 404 error. What is the reason of this? I've defined both rules but the system seems not to recognize the pattern - maybe I've mispelled something? Maybe I know nothing about url formatting (could be, it's my first time doing this stuff)?
A big thanks from a newbie programmer to everyone who finds the bug :)
The url-patterns are processed from top to bottom. Your third pattern ^(?P<slug>\w+) consumes everything, so about and posts is never reached.
An example: Django wants to find the view for the url about/. The patterns ^admin/ and ^$ do not match. But ^(?P<slug>\w+) does, because about starts with letters or numbers (the character sets contained in \w)
>>> import re
>>> re.search('^(?P<slug>\w+)', 'about/')
<_sre.SRE_Match object at 0x10b5b7be8>
So Django found a match, callBs views.view_post and finishes the request. That means, the more specific rule must come first. Better: avoid ambiguity.
You have to change position of url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),.
urlpatterns = [
# Examples:
# url(r'^$', 'blogas.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^posts/$', views.posts, name='posts'),
url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),
]
urlpatterns is list and order of urls is important.
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