Django admin custom view and urls - python

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

Related

Django Home url set to folder

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.

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

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

How to include app urls to project urls in Django 1.8?

I have just started with django now and was configuring urls. I am able to map different urls like /posts, /posts/create etc. Somehow I am not able to configure root url, I am not sure what wrong I am doing. Here is my configuration :
urlpatterns = [
# Examples:
url(r'',homeViews.posts),
# url(r'^blog/', include('blog.urls')),
url(r'^posts/',homeViews.posts),
url(r'^createPost/',homeViews.createPost),
url(r'^createUser/',userViews.createUser),
url(r'^post/(?P<title>[a-z,A-Z]+)/$',homeViews.post),
]`
Cheers!
here is an example, my friend.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'home.views.home',name='index'),
url(r'^contactanos/$', 'home.views.contacto',name='contacto'),
url(r'^post/$', 'home.views.blog',name='blog')
]

Django urls.py bug - some patterns not working

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.

Categories