how to set admin.site.urls as home page in django - django

some question about django 1.10:
I am going to make a website and the home page should be a login page created by django,when i tried to make the admin login page as my site's home page,it failed,can anybody help me ?
my urls.py:
urlpatterns = [ url(r'^$', include(admin.site.urls)), ]
or
urlpatterns = [ url(r'^$', admin.site.urls), ]
both works wrong.
thx

For djanog version 2 & later, the syntax url() has been replaced & simplified with path()
So, if you have django version >=2 , use this:
urlpatterns = [
path('', admin.site.urls),
]
Read the docs for details.

Generally speaking, setting admin page as the main page is not a good idea but still, whatever the use case is, try this:
url(r'^', admin.site.urls),
Tested it on Django 1.9 but I think it'll work on Django 1.10 as well.
Also, don't forget to restart the server after making these changes.

Related

Issue with Django recognizing URL passed parameter

I am learning Django and am having an issue with some simple testing and passing URL parameters. Here is my urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('custnames', views.custnames, name='custnames'),
path('custdetail/<int:cust_id>/', views.cust_detail, name='cust_detail'),
]
And here is my views.py
def cust_detail(request, cust_id):
return HttpResponse('<p>Cust Detail View with cust_id {cust_id}</p>')
When I put this for my URL in my browser: http://localhost:8000/custdetail/1/
My output is:
Cust Detail View with cust_id {cust_id}
The "home" and "custnames" sections seem to work fine.
Any ideas on what I'm doing wrong here?
~Ed
Thanks to "minglyu" and "Melvyn". Adding the "f" worked.

Is there a way to debug Django urls?

I have a problem with Django urls that I cannot get to the bottom of. I have tried the recommendations given in the answers to this question, but they don't help.
<project>/urls.py
urlpatterns = [
...
path('duo/', include('duo.urls')),
path('users/', include('users.urls')),
]
duo/urls.py
urlpatterns = [
...
path('', include('users.urls')),
...
]
users/urls.py
urlpatterns = [
path('', views.SelectPartner.as_view(), name='select-partner'),
...
]
when I use the url http://192.168.1.138:8000/duo/ I get taken to the page http://192.168.1.138:8000/accounts/login/?next=/duo/ which does not exist.
I cannot think what is going on here because the word accounts does not exist anywhere in the project
Is there some tool that I can use to find out what is happening?
You have login required somewhere which sends you to default login page location
If you are going to use default authentication you can add these views up
I think you forget to use the app in URL and then further parameters. Then it will work by default authentication is available in URL if you are going to use authentication

How to make django urls dynamic

I want to make my urls dynamic. So far the problem occurs when any of the url is not listed among these urls the page shows not found 404 error.
For eg:
If i want to access the /dashboard i can easily do it because it is listed. Now i want to access follow-up tab after that which is in my sidebar /dashboard/follow-up i can do that too but when i want to access another tab lets say table so the url will be /dashboard/follow-up/table it will not be accessed.
I know it is not possible to add all possible combinations of URL because there would be many.
I am new to django. Please someone guide me how to solve the problem. I have provided my urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('accounts.urls')),
path('accounts/', include('django.contrib.auth.urls')),
path('', TemplateView.as_view(template_name='home.html'), name='home'),
path('dashboard/notifications', TemplateView.as_view(template_name='notifications.html'), name='notifications'),
path('dashboard/Login_request', TemplateView.as_view(template_name='Login_request.html'), name='Login_request'),
path('dashboard/follow-up/', TemplateView.as_view(template_name='follow-up.html'), name='follow-up'),
path('accounts/password_change/', TemplateView.as_view(template_name='password_change'), name='password_change'),
path('accounts/password_change/done/', TemplateView.as_view(template_name='password_change_done'), name='password_change_done'),
path('dashboard/settings/', TemplateView.as_view(template_name='settings.html'), name='settings'),
path('dashboard/', views.chart, name='dashboard'),
path('table/', views.offense, name='table'),
path('dashboard/user/', TemplateView.as_view(template_name='user.html'), name='user'),
]

Django breaks on urls containing "logout"

I'm working on a Django web-app which has user accounts, and so has login and logout functions.
What I would like to have is mywebsite.com/accounts/login/ be the login page. This is working as expected right now.
This issue is with logout- what I would like to have is mywebsite.com/accounts/logout/ logout the user and redirect them to the login page. This, however, doesn't appear to work.
Here is my url configuration in accounts/urls.py:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logout/$', auth_views.logout, name='logout'),
]
With this configuration, login works fine. But when I go to mywebsite.com/accounts/logout/, I am just immediately sent back to the page I'm currently on.
However, if I change the logout url:
from . import views
urlpatterns = [
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logMeOutPlease/$', auth_views.logout, name='logout'),
]
Then mywebsite.com/accounts/login works as intended and mywebsite.com/accounts/logMeOutPlease works as intended. Is there a reason the first configuration won't work?
If I however move the logout functionality to the top level (i.e just mywebsite.com/logout/), then it again works fine.
For reference, this is what is in the "top level" urls file:
urlpatterns = [
# other urls that I can't show here
url(r'^redirect/', include('mywebsite.apps.redirect.urls')),
url(r'^accounts/', include('mywebsite.apps.accounts.urls')),
url(r'^$', RedirectView.as_view(url='redirect/')),
url(r'^admin/', admin.site.urls),
]
I am using Django 2.0.6 and Python 3.5.2, and my laptop is on Ubuntu 16.04.
Ok, so from what I understand so far, that behavior is actually correct. Since, views logic was not provided in your question, I will assume that the views are currently not using any #login_required decorators and the logout view does what it says i.e. logout. So without seeing the views logic we assume there were no instructions about where to redirect to on successful logout and no decorator so opens the same page again.
So here's the advice.
In your settings.py file:-
Add:
LOGOUT_REDIRECT_URL = 'url_you_want_to_redirect_to_on_logout'
This will enable redirecting to the specified url whenever a user logs out.
And if it helps, similarly we also do have a
LOGIN_REDIRECT_URL = 'url_you_want_to_redirect_to_on_login'

CSRF verification failure when trying to upload an image from django-ckeditor on staging server

Django-ckeditor has an option to insert and upload images directly from the editor. This works on local/development machines, but on remote servers Django throws a 403 error, CSRF verification failed. This happens in the admin backend, and I am having no other similar problems. Does anyone know what I am doing wrong? Any help would be greatly appreciated. Thanks
I had this issue because of url(r'^sys/cke/', include('ckeditor.urls')) was included to urlpatterns after less restrictive pattern (namely, url(r'^', include('cms.urls'))).
Thus, when reverse url resolver was used to build absolute url for ckeditor_upload it worked properly, but when url resolver was to find proper view for request, the request went not to the ckeditor's view with #csrf_exempt, but to view with first matched pattern. The confusion was because the 403 Forbidden error message has no trace of the view being executed.
Issue is still open.
https://github.com/shaunsephton/django-ckeditor/issues/84
Better to exempt the csrf verification.
I don't know if you already solved it, but I got the same problem. The issue was related to the django version. So you need to add this to your urls.py:
if django.VERSION >= (1, 8):
urlpatterns = [
url(r'^', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
url(r'^ckeditor/', include('libs.ckeditor_uploader.urls')),
]
else:
from django.conf.urls import patterns
admin.autodiscover()
urlpatterns = patterns(
'',
url(r'^', include('blog.urls', namespace="blog")),
url(r'^admin/', include(admin.site.urls)),
url(r'^ckeditor/', include('libs.ckeditor_uploader.urls')),
)