Django Generic Views Date-Based URLconf - python

Trying to teach myself Django but running into a snag.
Generic Views seem to be a great idea but I personally find the documentation a little cryptic at times (maybe I'm being prissy).
So I have been trying to use the Date Based generics views in and specifically ArchieveIndexView.
I have even attempted following some non-djangoproject.com examples and still have problems.
I used the example provided at this site.
Here is my current project/urls.py.
I am also at this point, not worrying about pattern matching, just trying to get it to work.
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view('date_field': 'pub_date', 'queryset': Entry.objects.all())),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
With this setup I keep receiving a Invalid Syntax error at the line describing ArchiveIndexView class.
If I comment out this line the problem goes away. If I decouple the URLs to their appropriate app I get the same error.
The error suggest I just have something out of place, a comma or something but I have yet to conclude what it is.
Thank you!

use the below code
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view({'date_field': 'pub_date', 'queryset': Entry.objects.all()})),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
You seem to forget the {} brace required for dict in url(r'^$', ArchiveIndexView.as_view('date_field': 'pub_date', 'queryset': Entry.objects.all())),
line.

Ah. I solved my own question thanks to a little pushing from shiva.
The dictionary works but only for the extra_content argument.
It was just done like that on the website I was trying to copy and for extra content in the documentation so I kept overlooking that glaringly obvious problem.
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view(date_field='pub_date', queryset=Entry.objects.all())),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Just needed to sleep on it...

Related

Django: extend Admin for all users

I'm creating a multi user blog in Djano and would like to use the Admin view for all users to provide functionality to create, edit, and delete posts.
I found this tutorial which explains the use of Admin for non-staff users. However, I am getting an error
cannot import name user_admin_site
admin.py
from django.contrib import admin
from django.contrib.admin.sites import AdminSite
class UserAdmin(AdminSite):
pass
user_admin_site = UserAdmin(name='user')
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from myapp.admin import user_admin_site
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include(user_admin_site.urls)),
url(r'^', include('myapp.urls')),
)

Django's admin documentation generator not working

I installed docutils.
Then included 'django.contrib.admindocs' in my 'INSTALLED_APPS',
Then, added 'admin/doc' to my url.
When I go to admin/doc, I see the list of things like models, views, filters and etc. It is fine. But when I click on one of those, it says page not found.
Where did I go wrong?
i know this question is for long time ago but :
Add path('admin/doc/', include('django.contrib.admindocs.urls')) to your urlpatterns.
1
Make sure it’s included before the 'admin/' entry, so that requests to /admin/doc/ don’t get handled by the latter entry.
if you code like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('admin/doc/', include('django.contrib.admindocs.urls')),
]
change it to:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/doc/', include('django.contrib.admindocs.urls')),
path('admin/', admin.site.urls),
]
The Django admin documentation generator

Django import doesnt work when trying to import views

I have the following urls.py file:
from django.conf.urls import patterns, url
from base import views
urlpatterns = patterns('',
url(r'^$', 'views.index', name='index'),
url(r'^item/new', 'views.newItem', name='newItem'),
url(r'^item/submitted', 'views.itemSubmitted', name='itemSubmitted'),
)
This doesnt work, it gives me an ImportError message saying that there is no module named views. When i remove the second import line above and change the lines from views.viewname to base.views.viewname it works. Does someone know why the import is not working?
Your url route list statement is using string statements to define the location of the views. Django will attempt to lazy-load view methods when required, which can be great for strange situations where importing the view methods would cause import loops. If import loops aren't a problem (which they shouldn't be), you have two ways of doing this:
from django.conf.urls import patterns, url
from base import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^item/new', views.newItem, name='newItem'),
url(r'^item/submitted', views.itemSubmitted, name='itemSubmitted'),
)
or
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'base.views.index', name='index'),
url(r'^item/new', 'base.views.newItem', name='newItem'),
url(r'^item/submitted', 'base.views.itemSubmitted', name='itemSubmitted'),
)
In the former, you are passing the view method as a property for the route. In the latter, you are passing an import path to the view methods. Note that in the latter you do not need to provide an import statement for the view.
To cut down on repitition, you can also extract the repeated prefix 'base.views':
from django.conf.urls import patterns, url
urlpatterns = patterns('base.views',
url(r'^$', 'index', name='index'),
url(r'^item/new', 'newItem', name='newItem'),
url(r'^item/submitted', 'itemSubmitted', name='itemSubmitted'),
)

how to display a view in Django?

I'm totally new to Django, and I'm trying to understand how does it work (I'm more used to PHP and Spring frameworks.
I have a project called testrun and inside it an app called graphs, so my views.py looks like:
#!/usr/bin/python
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, World. You're at the graphs index.")
then, in graphs/urls.py:
from django.conf.urls import patterns, url, include
from graphs import views
urlpatterns = patterns(
url(r'^$', views.index, name='index'),
)
finally, at testrun/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'testrun.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^graphs/', include('graphs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
However, when I try to access http://127.0.0.1:8000/graphs/ I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/graphs/
Using the URLconf defined in testrun.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, graphs/, 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.
What am I doing wrong that I can't get that simple message to be displayed in the browser?
To expand on my comment, the first argument to patterns() function is
a prefix to apply to each view function
You can find more information here:
https://docs.djangoproject.com/en/dev/topics/http/urls/#syntax-of-the-urlpatterns-variable
Therefore in graphs/urls.py you need to fix the patterns call like so:
urlpatterns = patterns('', # <-- note the `'',`
url(r'^$', views.index, name='index'),
)

Django url Routing Error

I have a very basic url router in my django project:
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
admin.autodiscover()
urlpatterns = staticfiles_urlpatterns()
urlpatterns += patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^/?', include('customApp.urls')),
)
When I start the dev server and go to 127.0.0.1:8000/admin/, I get a ViewDoesNotExist at /admin/ error.
Here is the content of the exception:
Could not import customApp.views.event. View does not exist in module customApp.views.
I've already tried re-ordering the urls (I have no idea how that would help, but I tried it anyway) and changing r'^/?' to r'^/'.
When I comment out the last url, the admin page works again.
Here's the customApp.urls code:
from django.conf.urls import patterns, include, url
import django.contrib.auth.views
import django.contrib.auth
urlpatterns = patterns('customApp.views',
url(r'^$', 'index'),
url(r'^rest/v1/event/add/$', 'event'),
url(r'^rest/v1/reports/$', 'reports'),
)
urlpatterns += patterns('',
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
)
It is simple buddy. Django cannot find customApp views. Please ensure whatever views you have got in urls.py, it should exist.
Thanks

Categories