django-cms and admin interface confilct - python

I have created custom index view. urls.py:
url(r'^', include('cms.urls')),
url(r'^', 'myapp.views.index', name='index'),
in views.py:
from cms.utils import get_template_from_request
def index(request):
template = get_template_from_request(request)
.....
return render(request, template)
When i try to access django admin 127.0.0.1:8000/admin i get an error
'NoneType' object has no attribute 'pk'
because in my index.html is templatetag {% product_list request.current_page %} which requires current_page to be in request. I think this happens because django renders my index page in django admin, where it shouldn't. What can i do to fix this?

I think the easiest way to fix your problem is to include the urls of the admin site before those 'index' and 'cms' as explained in the Django documentation site. Your url patterns in the urls.py file would be something like this:
...
url(r'^admin/', admin.site.urls),
url(r'^', include('cms.urls')),
url(r'^', 'myapp.views.index', name='index'),
...

Previously i had url(r'^myapp/', include('myapp.urls')), changed to url(r'^', include('myapp.urls')) and it's working.

Related

NoReverseMatch found in django for app homepage

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"),
]

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')),
]

Why am I getting a 404 error when trying to access my website's domain name, yet if I domain name/home it works?

Okay... let's try to explain things clearly. I've used Python Django to create a dynamic webpage/web-app. After completing the website I have published it using DigitalOcean and have successfully attached my purchased domain name to the name server of DigitalOcean. When I access my website, ordinanceservices.com, i get an error 404; however, if I type ordinanceservices.com/home it works as it should and displays the home page. How, by editing the python files, can I have it to where ordinanceservices.com will display the home page as opposed to error 404? I feel like there's something that I am doing that is fundamentally wrong regarding my .urls structure and thus a rewrite/redirect in the nginx config should not be necessary.
Here is the specific error:
Page not found (404)
Request Method: GET
Request URL: http://ordinanceservices.com/
Using the URLconf defined in django_project.urls, Django tried these URL patterns, in this order:
^admin/
^ ^home/ [name='home']
^ ^contact/ [name='contact']
^ ^services/ [name='services']
The current URL, , didn't match any of these.
I somewhat understand what is happening here though I do not know how to fix this. Next I will provide my .urls files for each folder that contains such:
/django_project urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
)
/company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
/company views.py
from django.shortcuts import render
def home(request):
return render(request, 'company/home.html')
def contact(request):
return render(request, 'company/contact.html')
def services(request):
return render(request, 'company/services.html')
What I am aiming to do, without needing to redirect the main URL using the nginx config files to do so, is to edit my urls and views structure of my Python files to ensure that the normal URL, ordinanceservices.com, will actually display a page; preferably the home page of my webpage.
I have a hunch that it has to do with the fact that I do not have a views.index for the r'^admin/' to reach to. I am not certain but I have been trying to figure this out for hours. Does anyone have a clue what I can do to fix this?
You haven't defined anything at the root url. Add one more line to your company urls.py so it becomes
//company urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^home/', views.home, name='home'),
url(r'^contact/', views.contact, name='contact'),
url(r'^services/', views.services, name='services'),
]
First of all, check if you have added www.yourdomain.com or yourdomain.com to ALLOWED_HOST = ['www.yourdomain.com','yourdomain.com'] in your settings.py
and then in your company/urls.py do this
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^services/$', views.services, name='services'),
]
and in your main urls.py add this code
from django.conf.urls import url,include
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('company.urls')),
]

password_change_done not using custom template in Django

I used Django's auth_views.password_change to allow a user to change their password. It uses a custom template and it works well, but the problem I'm having is that it redirects to Django's password change successful admin template instead of the custom template I declared.
urls.py
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
app_name = 'users'
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name='index'),
url(r'^add-user/$', views.CreateUserView.as_view(), name='add-user'),
url(r'^search/$', views.UserSearchView.as_view(), name='search-users'),
url(r'^login/$', views.LoginView.as_view(), name='login'),
url(r'^logout/$', views.LogoutView.as_view(), name='logout'),
url(r'^(?P<pk>\d+)/settings/update$', views.UpdateAccountView.as_view(), name='update-account'),
url(r'^settings/change/$', auth_views.password_change, {'template_name': 'users/forms/change-password.html'},
name='change-password'),
url(r'^settings/change-done/$', auth_views.password_change_done,
{'template_name': 'users/forms/change-password-done.html'}, name='change-password-done'),
url(r'^(?P<pk>\d+)/delete-user/$', views.DeleteUserView.as_view(), name='delete-user'),
Any Ideas what I missed?
In INSTALLED_APPS ensure that your app with the custom templates is specified before admin.
Django searches for the template in order.
You should use:
url(r'^settings/change/$', auth_views.password_change, {'template_name': 'users/forms/change-password.html',post_change_redirect:'change-password-done'},name='change-password'),
Without post_change_redirect default template will be loaded in that case.
Quite long since this question was asked, but the above answers didn't work for me and this did. In urls.py:
Make sure this import is there:
from django.contrib.auth.views import ..., password_change_done, ...
Then remove url(r'^', include('django.contrib.auth.urls')) from the URLs and override the password_change_done view with your custom template. Again, I also had the above suggestions already implemented in my code.

How to set a html page in Django?

I started to follow this tutorial and it teaches how to load the index.html, but now I need to develop a login.html page, but it seems not be working properly.
urls.py
urlpatterns = patterns('',
url(r'^$', 'provisioning.views.home', name='home'), <- it works!
url(r'^$', 'provisioning.views.login', name='login'), <- doesn't work..
url(r'^admin/', include(admin.site.urls)),
)
views.py
def login(request):
return render(request, 'login.html')
How to setup the pages to be loaded by django ?
Is there another and better way in doing this ?
You have the same URLs for both home and login. The regex pattern r'^$' specifies that nothing comes after your local host. Because they are the same and Django checks URLs sequentially, only the first url and view is called. Try adding a different url for login.
url(r'^login/$', 'provisioning.views.login', name='login')

Categories