Django url not finding a template using CreateView in Class-Based Views - python

I have created a page where a user can add a new item (notes in this case) and I am making use of CBV which I have recently started learning.
This is my model form
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = ('title', 'note', 'tags')
This is the view in views.py
class NoteCreate(CreateView):
model = Note
form_class = NoteForm
template_name = "add_note.html"
Then this is the url as I used in the urls.py of the app
from django.conf.urls import patterns, url
from . import views
from madNotes.views import NoteCreate, NoteIndex,
urlpatterns = patterns(
'',
url(r'^notes/add/$', NoteCreate.as_view(), name="new_note"),
url(r'^$', NoteIndex.as_view()),
url(r'^(?P<slug>\S+)/$', views.NoteDetail.as_view(), name="entry_detail"),
)
NB: I used the same url as the main page at 127.0.0.1:8000 in the projects urls.py file and it worked.
I have seen several tutorials and even the docs and can't seem to find what I am doing wrong. Will I also need to add a function in order for it to be saved in the db or the CBV will do it all?
EDit: The error I get is this
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/notes/add/
Here is the project's urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from MadNotez import settings
from registration.backends.default.views import RegistrationView
from madNotes.forms import ExRegistrationForm
if settings.DEBUG:
import debug_toolbar
urlpatterns = patterns('',
url(r'^__debug__/', include(debug_toolbar.urls)),
url(r'accounts/register/$', RegistrationView.as_view(form_class = ExRegistrationForm), name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^admin/', include(admin.site.urls)),
url('^markdown/', include('django_markdown.urls')),
url('^notes/', include('madNotes.urls')),
#url(r'^$', views.NoteCreate.as_view(), name="new note"), when I used it here it worked
)

you say that is the urls.py of the app, which means it is included by the project's urls.py.
As you show now, all the app's URIs go under the notes prefix:
url('^notes/', include('madNotes.urls')),
so as things stand at present the correct URI for the page is
http://127.0.0.1:8000/notes/notes/add/
In order to clean things up a bit, I'd suggest to modify the app's urls.py to
url(r'^add/$', NoteCreate.as_view(), name="new_note"),
so that the page can be reached at
http://127.0.0.1:8000/notes/add/
This way all the app's pages/services are available under the notes prefix and with a simple name that is consistent with their action (i.e. add, delete, etc)

Related

I'm getting 404 error on every page in django

I'm developing my first app in Django and facing some issues. The server runs smoothly and I can operate the admin panel without any issues.
However, all of the app pages including the default homepage show a "404 not found" error.
I have created a templates directory in project root
updated this line in settings.py for templates,
'DIRS': [os.path.join(BASE_DIR,'templates')],
View for the app
from django.shortcuts import render
from .models import *
# Create your views here.
def customers(request):
customers = Customer.objects.all()
return render(request, "customers.html", {'customers': customers})
urls for the app
from django.urls import path from . import views
urlpatterns = [path('customers',views.customers, name='customers')]
urls for the project
from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static
urlpatterns = [
path('trips/',include('trips.urls')),
path('customers/',include('customers.urls')),
path('drivers/',include('drivers.urls')),
path('vehicles/',include('vehicles.urls')),
path('admin/', admin.site.urls), ]
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Help is appreciated
I can't see any problem with your case, you should be able to load http://localhost:8000/customers/customers. Maybe double customers was not you intention, then remove one of them (one in main urls.py one in app's).
Also when Django debug mode is active and you get a 404 because of URL mismatch, it shows you a list of URLs you have. To see it navigate to a non existing URL like http://localhost:8000/aaa. Then if you see customers/ there try http://localhost:8000/customers/. Go step by step to find the problem.

Django Path To Resource Failure?

When I try a GET request on one of my API endpoints it can't find the endpoint.
urls.py file looks like this
from django.urls import path, include
from django.contrib import admin
from api.resources import NoteResource
note_resource = NoteResource()
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include(note_resource.urls)),
]
api.resources looks like this
from tastypie.resources import ModelResource
from api.models import Note
class NoteResource(ModelResource):
class Meta:
queryset = Note.objects.all()
resource_name = 'note'
Any idea why this is happening?
Solution: It appears that http://127.0.0.1:8000/api/note/ works properly.. why would this be?
You should also have one url entry in note_resource.urls for only /api request. Something similar to
path('api/', APIClass).
But, you never need that endpoint. Because, /api does not represent any actual request in your system.
I rather suggest to have following endpoints :
path('api/notes/',include(note_resource.urls))
in your main urls.py.
So that you can have multiple urls in main urls.py file representing each app.
path('api/<APP_NAME>(s)/',include(<APP_NAME>.urls))
And, you will manage other endpoints in your app urls.py file:
# Create a new Note
path(
'create/',
NoteCreate.as_view()
),

Django urls.py import not detecting

I have a working api, and I am writing a UI to the API, as a separate application in the same project. My project urls.py looks like this
from django.conf.urls import *
import search
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^api/search$', search.validation),
url(r'^ui', include('ui.urls')),
)
My UI app's urls.py looks like this
from django.conf.urls import *
import views
urlpatterns = patterns('',
(r'^ui/$', views.search_template),
)
However, when I am trying to access with my browser(domain.com:8000/ui), I am getting an error.
Using the URLconf defined in api.urls, Django tried these URL patterns, in this order:
^api/search$
^ui ^ui$
The current URL, ui, didn't match any of these.
But if I use the below mapping in the main project's urls.py, it works.
(r'^ui$', ui.views.user_template),
I tried clearing the urls.pyc to make sure it is not stale, but it still persists. Please let me know what am I doing wrong.
You shouldn't repeat ui regex in the app's urls.py:
urlpatterns = patterns('',
(r'^$', views.search_template),
)

What more do I need to do to have Django's #login_required decorator work?

I am trying to use Django's account system, including the #login_required decorator. My settings.py file includes django.contrib.auth and I have done a syncdb.
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/accounts/login/?next=/
Using the URLconf defined in dashboard.urls, Django tried these URL patterns, in this order:
^$ [name='home']
The current URL, accounts/login/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
I see the above after trying to #login_required-decorate my home view.
It seems to be choking because it is redirected to accounts/login/, which I have not prepared for in my urls.py.
What can I add to urls.py or elsewhere so that the login_required decorator will do its usual behaviour?
Thanks,
Set the LOGIN_URL in your settings. The default value is '/accounts/login/'
The decorator also takes an optional login_url argument:
#login_required(login_url='/accounts/login/')
And, from the docs:
Note that if you don’t specify the login_url parameter, you’ll need to
ensure that the settings.LOGIN_URL and your login view are properly
associated. For example, using the defaults, add the following line to
your URLconf:
(r'^accounts/login/$', 'django.contrib.auth.views.login'),
path('accounts/login/', admin.site.urls),
Add this line in your urls.py project folder. Then it will work fine.
from django.contrib.auth.decorators import login_required
#login_required(login_url='/accounts/login/')
Add above two lines in your views.py file.
What worked for me in Django 2.2.1 - include re_path('^accounts/', admin.site.urls), in my project urls.py:
urls.py
from django.conf import settings
from django.conf.urls import include
from django.conf.urls import re_path
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
re_path('^accounts/', admin.site.urls),
]
And in my views.py:
views.py
from django.contrib.auth.decorators import login_required
from django.views.generic import TemplateView
#method_decorator(login_required, name='dispatch')
class HomePageView(TemplateView):
"""
Home Page View
"""
template_name = 'amp/home.html'
Hope that helps.
UPDATE: To avoid warnings from django in regards to admin urls being loaded twice, I used a redirect instead in urls.py:
urls.py
urlpatterns = [
re_path('^accounts/', admin.site.urls),
re_path(r'^admin/', RedirectView.as_view(url='/accounts/', permanent=True))
]
More on redirect view here.

Override AdminSite to append custom urls

I overrode default AdminSite class as described in manual, though it's too pure with information about this part over there.
My gs/admin.py file:
from django.contrib.admin import AdminSite
from django.conf.urls import patterns, url
from gs.views import *
class AdminSiteGs(AdminSite):
def get_urls(self):
urls = super(AdminSiteGs, self).get_urls()
urls += patterns('',
url(r'^my_admin_view/$', self.admin_view(my_admin_view))
)
return urls
admin_site_gs = AdminSiteGs()
gs it's my application and project name.
gs/urls.py file:
from django.conf.urls import patterns, include, url
from page import views
from gs.admin import admin_site_gs
urlpatterns = patterns('',
url(r'^admin/', include(admin_site_gs.urls)),
)
and I have application named page, where I place admin.py file:
from gs.admin import admin_site_gs
from page.models import Page, Menu
from django.contrib import admin
class PageAdmin(admin.ModelAdmin):
list_display = ('name', 'url', 'page_type')
class MenuAdmin(admin.ModelAdmin):
list_display = ('name', 'code')
admin_site_gs.register(Page, PageAdmin)
admin_site_gs.register(Menu, MenuAdmin)
So nothing here is working =( Neither /admin/my_admin view ( it returns 404 ), nor main admin page /admin. I don't see my models I registered in page/admin.py file.
It may sounds fun, but I tried all staff working in 3-4 hours =)) As you might guess I'm completely newbie both in Django and Python... All I want to know now is how to append custom URLs and views to my overriden class of AdminSite?
I removed autodiscover method, so now seems Django doesn't see nothing about file page/admin.py.
But the first question is more interesting, why I've had 404 error on trying to access the /admin/my_admin page ...
PS why my greeting at the beginning has been cutted o_O
According to the docs, any URL patterns you define for custom admin views must be occur before the admin patterns: https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls
Try:
def get_urls(self):
urls = super(AdminSiteGs, self).get_urls()
my_urls = patterns('',
url(r'^my_admin_view/$', self.admin_view(my_admin_view))
)
return my_urls + urls
You shouldn't need to include these patterns like this:
urlpatterns = patterns('',
url(r'^admin/', include(admin_site_gs.urls)), # not needed
)
In my case, I had to override the default 'add url' in order to redirect to a custom Django admin page when clicking '+Add' button in the admin.
So if I just override get_urls() in the way #Brandon said, it will return a list with a duplicate 'add' url (the custom one and the one retrieved from the super).
def get_urls(self):
info = self.model._meta.app_label, self.model._meta.model_name
urls = super(RetailerAdmin, self).get_urls()
# We need to remove the original 'add_url' in order to use the custom one.
urls.remove(urls[1])
custom_url = [
url(r'^batch/$', self.admin_site.admin_view(self.batch_upload_retailers),
name='%s_%s_add' % info),
]
return custom_url + urls
To solve this, I removed the original 'add' url (notice that the 'add' url is always in position 1).

Categories

Resources