What is equivalent of "direct to template" in class based view in django - django

I used to do this when function based generic views were there
(r'^foo/$', direct_to_template, {'template': 'foo_index.html'}),
what is the equvalent of this in class based views so that i don't need to define anything in my views.py

That would be the TemplateView, and you use it like:
from django.conf.urls import patterns, url
from django.views.generic.base import TemplateView
urlpatterns = patterns('',
url(r'^foo/$', TemplateView.as_view(template_name='foo_index.html')),
)

Related

NameError name 'Views' is not defined

from django.conf.urls import url, patterns, include
from django.contrib import admin
from django.views.generic import TemplateView
from collection import *
#from collection.views import index,thing_detail,edit_thing
urlpatterns = [
url(r'^$', views.index, name='home'),
url(r'^about/$',TemplateView.as_view(template_name='about.html'),name='about'),
url(r'^contact/$',TemplateView.as_view(template_name='contact.html'),name='contact'),
url(r'^things/(?P<slug>[-\w]+)/$', 'views.thing_detail' ,name='thing_detail'),
url(r'^things/(?P<slug>[-\w]+)/edit/$', 'views.edit_thing',name='edit_thing'),
url(r'^admin/', include(admin.site.urls)),
]
After running the server there is an error "NameError: name 'views' is not defined"
Any help ??
You aren't importing your own views.
Try adding this to your urls.py:
from . import views
Or if you are importing them from a specific app, try replacing . with the app name
First thing I notice is the import *, realize that this will/can cause confusion for other Developers reading your scripts. Python has a methodology that insists that explicit is better than implicit. Which in this senario means you should be explicit about what you are importing.
from django.conf.urls import url, patterns, include
from django.contrib import admin
from django.views.generic import TemplateView
from collection import views as collection_views
urlpatterns = [
# Function Based Views
url(r'^$', collection_views.index, name='home'),
url(r'^things/(?P<slug>[-\w]+)/$', collection_views.thing_detail ,name='thing_detail'),
url(r'^things/(?P<slug>[-\w]+)/edit/$', collection_views.edit_thing,name='edit_thing'),
# Class Based Views
url(r'^about/$',TemplateView.as_view(template_name='about.html'),name='about'),
url(r'^contact/$',TemplateView.as_view(template_name='contact.html'),name='contact'),
# Admin
url(r'^admin/', include(admin.site.urls)),
]
Here instead of importing everything from collection I'm importing just your views and assigning them to a variable. Then using that variable in the URL definitions.
Be sure to import your views by
specifying its location and the methods inside the view to be imported on your urls.py.
from . collection import *
(line above means from current location find collection.py and import everything on it)
Happy coding!

what's wrong with my urls.py in this example?

I just installed userena, and had the example working from the tutorial, but as soon as I added in a single line in URLS.py, I'm getting an error. In the example below, I added the line mapping the home function from views.py
Now the issue I'm having is that when I go to 127.0.0.1/8000, I get TypeError: string is not callable, but then oddly, if I go to accounts/signup or accounts/signin, I am getting the template that should be appearing if i go to 127.0.0.1/8000.
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.views.generic import TemplateView
from accounts import views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Here is my accounts/views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect
def home(request):
return render('homepage.html')
You need to remove the quotes in the url and import that view
from accounts.views import home
urlpatterns = patterns('',
url(r"^$", home),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
You can steel use the strings in the url() but you must use the format 'app.views.viewname'
urlpatterns = patterns('',
url(r"^$", 'accounts.views.home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
Or name the module in the first argument as string to patterns()
urlpatterns = patterns('accounts.views',
url(r"^$", 'home'),
url(r'^admin/', include(admin.site.urls)),
(r'^accounts/', include('userena.urls')),
)
the issue was I forgot to include the request in the return render.
The correct answer is that render is being called incorrectly. Actually, the views.py file would raise a SyntaxError, but we'll let that slide :)
# views.py
from django.shortcuts import render
def home(request):
return render(request, 'homepage.html')

Django 1.5: django-registration email a unique field?

I use Django 1.5 + django-registration 0.9...
How to make email field unique in model User?
from registration.forms import RegistrationFormUniqueEmail
url(r'^accounts/register/$', 'registration.views.register',
{'form_class': RegistrationFormUniqueEmail,
'backend': 'registration.backends.default.DefaultBackend'},
name='registration_register'),
This solution is not suitable
Could not import registration.views.register. View does not exist in module registration.views.
Many thanks, #Alasdair
How to use different view for django-registration?
urls.py:
from registration.backends.default.views import RegistrationView
from registration.forms import RegistrationFormUniqueEmail
class RegistrationViewUniqueEmail(RegistrationView):
form_class = RegistrationFormUniqueEmail
urlpatterns = patterns('',
....
url(r'^user/register', RegistrationViewUniqueEmail.as_view(),
name='registration_register'),
...
Another approach is to pass the form class into the as_view() method directly as in the following example:
url(r'^user/register/$', RegistrationView.as_view(
form_class=RegistrationFormUniqueEmail),
name='registration_register'),
A complete urls.py for this:
from django.conf.urls import patterns, include, url
from registration.forms import RegistrationFormUniqueEmail
from registration.backends.default.views import RegistrationView
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'django_registration_demo.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^accounts/', include('django.contrib.auth.urls')),
# enable unique email registration feature
url(r'^accounts/register/$',
RegistrationView.as_view(form_class=RegistrationFormUniqueEmail),
name='registration_register'),
url(r'^accounts/', include('registration.backends.default.urls'))
)
I've also create a complete demo for django-registration, which enables unique email feature , see:
https://github.com/xiaohanyu/django-registration-demo

Django: How can i get the url of a template by giving the namespace?

While i'm rendering a template i would like to retrieve the url of the template by giving the namespace value and not the path. For example instead of this:
return render(request, 'base/index.html', {'user':name})
i would like to be able to do the following:
from django.shortcuts import render
from django.core.urlresolvers import reverse
return render(request, reverse('base:index'), {'user':name})
but the above produces an error. How can i do it? Is there any way to give the namespace to a function and get the actual path?
Extended example:
- urls.py
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^', include('base.urls', namespace='base')),
)
- app base: urls.py
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('base.views',
url(r'^/?$', 'index', name='index'),
)
- app base: views.py
from django.shortcuts import render
from django.core.urlresolvers import reverse
def homepage(request):
'''
Here instead of 'base_templates/index.html' i would like to pass
something that can give me the same path but by giving the namespace
'''
return render(request, 'base_templates/index.html', {'username':'a_name'})
Thanks in advance.
Template names are hard coded within the view. What you can also do is that you can pass the template name from the url pattern, for more details see here:
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('base.views',
url(r'^/?$', 'index',
{'template_name': 'base_templates/index.html'},
name='index'),
)
Then in view get the template name:
def index(request, **kwargs):
template_name = kwargs['template_name']

"No module named simple" error in Django

ImportError at /
No module named simple
Django Version: 1.5.dev20120710212642
I installed latest django version. I am using
from django.views.generic.simple import redirect_to
in my urls.py. What is wrong? Is it deprecated?
Use class-based views instead of redirect_to as these function-based generic views have been deprecated.
Here is simple example of class-based views usage
from django.conf.urls import patterns, url, include
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^about/', TemplateView.as_view(template_name="about.html")),
)
Update
If someone wants to redirect to a URL, Use RedirectView.
from django.views.generic import RedirectView
urlpatterns = patterns('',
(r'^one/$', RedirectView.as_view(url='/another/')),
)
this should work
from django.conf.urls import patterns
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'some-url', RedirectView.as_view(url='/another-url/'))
)
Yes, the old function-based generic views were deprecated in 1.4. Use the class-based views instead.
And for the record (no relevant example currently in documentation), to use RedirectView with parameters:
from django.conf.urls import patterns, url
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^myurl/(?P<my_id>\d+)$', RedirectView.as_view(url='/another_url/%(my_id)s/')),
)
Please note that although the regex looks for a number (\d+), the parameter is passed as a string (%(my_id)s).
What is still unclear is how to use RedirectView with template_name in urls.py.