Adding simple views in django - django

I am getting started on django and this is how my view is looking like
from django.template import Context, loader
from datetime import datetime
from django.http import HttpResponse
def hello_view(request):
""" Simple Hello World View """
t = loader.get_template('helloworld.html')
c = Context({
'current_time': datetime.now(),
})
return HttpResponse(t.render(c))
def detail_view(request):
return HttpResponse("You're looking at detail view")
My urls.py file looks like this
from django.conf.urls import patterns, include, url
from posted.views import hello_view
#from posted.views import detail_view
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', view=hello_view, name='hello_page'),
#url(r'^$', view=detail_view, name='detail_page'),
# url(r'^posts/', include('posts.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)
When i run the server and visit http://example.com:8000/ ,the hello_view gets displayed.I want my urls to be of the form http://example.com:8000/hello or http://example.com:8000/detail. Do i need .htaccess to achieve that?.

I have fixed the issue somewhat with this while i study what Daniel suggested i look at.
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
(r'^detail/', TemplateView.as_view(template_name="detail.html")),
# url(r'^posts/', include('posts.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)

Related

Django NoReverseMatch Error

I'm trying to develop a blog script with Django. But when I want to show post links, I get NoReverseMatch error.
My views.py
# -*- coding: utf-8 -*-
# Create your views here.
from .models import Yazi, Yorum, Kategori
from django.http import HttpResponse, Http404
from django.shortcuts import render_to_response
from django.template import RequestContext, loader
from django.contrib.sites.models import Site
def Home(request):
try:
posts = Yazi.objects.filter(yayinlanmis=True).order_by('-yayinlanma_tarihi')
except Yazi.DoesNotExist:
raise Http404
site = Site.objects.get_current()
c = RequestContext(request,{
'posts':posts,
'site':site
})
return render_to_response('Penguencik_Yazilar/yazi_list.html', c)
def Detail(request, slug):
post = Yazi.objects.get(slug=slug)
site = Site.objects.get_current()
c= RequestContext(request,{
'posts':post,
'site':site
})
return render_to_response('Penguencik_Yazilar/yazi_detail.html',c)
Urls.py in application folder.
from django.conf.urls import patterns, url
import views
urlpatterns = patterns('',
url(r'^$', views.Home,name='index'),
url(r'^/makale/(?P<slug>[0-9A-Za-z._%+-]+)$', views.Detail,name='detail'),
)
Urls.py in project folder
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('Penguencik_Yazilar.urls',namespace='blog')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And template code. What am I doing wrong?
{% load url from future %}
...
Read
Try to change this:
Read
to:
Read
Cause your view expect slug keyword here (?P<slug>[0-9A-Za-z._%+-]+).

Django - Why doesn't reverse work?

I'm trying to make a view redirect to another view so user's can't access it without a condition being met. In this case, a successful password change. I also want to achieve this using HttpResponseRedirect.
Now, I've never used reverse before, but I figured that this is the way to do that. Problem is that I can't get reverse to work. I get the following error:
ViewDoesNotExist at /account/password/
Could not import findadownload.main.views.foo. View does not exist in module findadownload.main.views.
# URLS
from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
#SANDY
from findadownload.sandy.views import *
#TRIVIARIA
from findadownload.trivia.views import *
from findadownload.accounts.views import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
url(r'^$', 'findadownload.main.views.presidium_home', name='presidium_home'),
(r'^web/$', 'findadownload.main.views.web'),
(r'^web/developers/$', sandy_developers),
#SANDY (for findadownload!)
(r'^get/sandy/$', get_sandy),
(r'^get/sandy/download/$', get_sandy_download),
(r'^sandy/register/$', sandy_registration),
(r'^sandy/complete/$', sandy_complete),
(r'^web/shared/$', sandy_shared_files),
(r'^web/remove/all/$', sandy_remove_all),
(r'^web/remove/(?P<file_id>\w+)/$', sandy_remove_file),
(r'^about/$', 'findadownload.main.views.about'),
(r'^pytech/$', 'findadownload.main.views.pytech'),
(r'^pytech/aboutus$', 'findadownload.main.views.pytech_about'),
(r'^pytech/services$', 'findadownload.main.views.pytech_services'),
(r'^pytech/support$', 'findadownload.main.views.pytech_support'),
(r'^pytech/contactus$', 'findadownload.main.views.pytech_contact'),
(r'^pytech/sitemap$', 'findadownload.main.views.pytech_sitemap'),
(r'^pytech/search/$', 'findadownload.main.views.pytech_search'),
(r'^presidium', 'findadownload.main.views.presidium_home'),
#TRIVIARIA
(r'^register/$', register),
(r'^login/$', login_view, {'template': 'trivia/registration/login.html'}),
(r'^logout/$', logout_view),
(r'^trivia/$', submit_question, {'template': 'trivia/trivia_questions.html'}),
(r'^challenges/$', submit_question, {'template': 'trivia/trivia_challenges.html'}),
(r'^question/thanks/$', question_thanks),
(r'^question/answer/$', submit_answer),
(r'^question/answer/result/(\d+)/(.*)/$', answer_result),
(r'^question/answer/challenge/$', challenge_answer),
(r'^account/$', upload_avatar),
(r'^account/password/$', password_change),
url(r'^account/password/success/$', 'findadownload.accounts.views.password_success'),
(r'^(?P<username>\w+)/$', view_user_profile, {'template': 'trivia/trivia_view_user_questions.html'}),
(r'^(?P<username>\w+)/trivia/$', view_user_profile, {'template': 'trivia/trivia_view_user_questions.html'}),
(r'^(?P<username>\w+)/challenges/$', view_user_profile, {'template': 'trivia/trivia_view_user_challenges.html'}),
#FILE SHARING
(r'^web/share/(?P<file_id>\w+)/$', sandy_download),
(r'^web/report/(?P<file_id>\w+)/$', sandy_file_report),
(r'^web/like/(?P<file_id>\w+)/$', like_download),
#OTHER
(r'^web/profile/$', 'findadownload.main.views.web_profile'),
(r'^web/edit/$', 'findadownload.main.views.web_edit'),
(r'^trivia/$', 'findadownload.main.views.trivia_home', {'template': 'trivia/registration/index.html'}),
(r'^web/get/$', 'findadownload.main.views.web_get'),
(r'^web/progress/$', 'findadownload.main.views.web_progress'),
(r'^web/foo/$', 'findadownload.main.views.foo'),
(r'^web/search/$', 'findadownload.main.views.web_search'),
(r'^web/logout/$', 'findadownload.main.views.web_logout'),
(r'^web/resend/$', 'findadownload.main.views.web_resend'),
(r'^web/home/$', 'findadownload.main.views.web_home'),
(r'^web/history/$', 'findadownload.main.views.web_history'),
(r'^web/verify/(?P<link>\w+)/$', 'findadownload.main.views.web_activate'),
(r'^web/help/(?P<username>\w+)/$', 'findadownload.main.views.web_help'),
(r'^web/welcome/(?P<username>\w+)/$', 'findadownload.main.views.welcome'),
(r'^web/terms/$', 'findadownload.main.views.web_terms'),
(r'^web/privacy/$', 'findadownload.main.views.web_privacy'),
(r'^web/signup/$', 'findadownload.main.views.web_signup'),
(r'^web/login/$', 'findadownload.main.views.web_login'),
(r'^test$', 'findadownload.main.views.test'),
# url(r'^findadownload/', include('findadownload.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
if 1:
urlpatterns += patterns('',
url(r'^uploads/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
'show_indexes': True,
}),
)
urlpatterns += staticfiles_urlpatterns()
# VIEWS
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from findadownload.accounts.forms import *
from django.contrib.auth.models import User
from django.contrib import auth
from findadownload.accounts.models import *
from django.core.urlresolvers import reverse
from findadownload.trivia.models import *
def password_success(request):
return render_to_response('trivia/trivia_password_success.html')
def password_change(request):
if request.user.is_authenticated():
# if for is submitted
if request.method == 'POST':
form = PasswordForm(user=request.user, data=request.POST)
# for the avatar part
if form.is_valid():
cd = form.cleaned_data
request.user.set_password(cd['confirm_password'])
request.user.save()`enter code here`
# redirect to profile page after POST
return HttpResponseRedirect(reverse('findadownload.accounts.views.password_success'))
else:
form = PasswordForm(user=request.user)
# render empty form
return render_to_response('trivia/trivia_password_change.html', {'form': form},
context_instance=RequestContext(request))
else:
return HttpResponseRedirect('/login/?next=%s' % request.path)
You have to specify name kwargs in you url, in this case :
url(r'^account/password/success/$', 'findadownload.accounts.views.password_success', name = 'password_success')
Then use reverse like this :
return HttpResponseRedirect(reverse('password_success'))
What you pass in reverse is the name kwarg on urlpatterns.
EDIT:
Can i see the rest of your urls.py. I think the problem is not on reverse, because Django can't find view named foo.

Why an extra slash is getting added to urls?

I've created a blog using django and hosted it on dotcloud http://www.honeybunny.dotcloud.com/blog/ its working fine on the localhost but when i try to access it online an extra slash is added to the urls what could be the reason ?
www.sitename.com/admin/
becomes
www.sitename.com//admin/
my urls.py is as following
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
import blog
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$',redirect_to,{'url':'/blog'}),
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/',include('blog.urls')),
)
Also submitting the login form on the admin redirects me to http://admin/ while in the local environment it works perfectly fine .
UPDATE : My problem seems remarkably similar to the one described here .
From django tutorials the url file should look like the one below.
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^articles/2003/$', 'news.views.special_case_2003'),
(r'^articles/(\d{4})/$', 'news.views.year_archive'),
(r'^articles/(\d{4})/(\d{2})/$', 'news.views.month_archive'),
(r'^articles/(\d{4})/(\d{2})/(\d+)/$', 'news.views.article_detail'),
)
from django.conf.urls.defaults import patterns, include, url
from django.views.generic.simple import redirect_to
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'honeybunny.views.home', name='home'),
# url(r'^honeybunny/', include('honeybunny.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^$',redirect_to,{'url':'/blog'}),
url(r'^admin/$', include(admin.site.urls)),
)
Is my urls.py file
I have deleted import blog line because I don't have blog module.
It seemed to be a problem with dotcloud the same code worked without any errors when i uploaded it to epio

NameError:Admin not found in django

The urls.py of the project is this
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Sdr.views.home', name='home'),
# url(r'^Sdr/', include('Sdr.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
(r'', include('Sdr.sdr.urls')),
)
The urls.py of the app looks like this
# Import django modules
from django.conf.urls.defaults import *
from django.contrib import admin
# Import custom modules
import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'', include('Sdr.sdr.urls')),
)
The error I am getting is exception found is
Django Version: 1.3
Exception Type: NameError
Exception Value:
name 'admin' is not defined
You forgot to import admin in the project's urls.py. Read harder.
Uncomment the following in your project's urls.py.
from django.contrib import admin
admin.autodiscover()
Faced with the same problem in tutorial.
Try to add in models.py:
from django.contrib import admin

Django Admin application no working on URL

In my urls file I have configured the Django admin application to run off the url /adminDJ/. However it doesn't run. It loads up my own admin stuff. Here is my urls.py:
(r'^admin/add/member/$', 'astonomyStuff.attendance.views.newMember'),
(r'^admin/add/$', 'astonomyStuff.attendance.views.addPage'),
(r'^admin/$', 'astonomyStuff.attendance.views.adminPage'),
(r'^adminDJ/$', include(admin.site.urls)),
(r'^talks/$', 'astonomyStuff.attendance.views.talksIndex'),
(r'^talks/past/$', 'astonomyStuff.attendance.views.viewAllTalks'),
(r'^members/$', 'astonomyStuff.attendance.views.viewMembers'),
(r'^members/(?P<member_number>[^/]+)/$', 'astonomyStuff.attendance.views.viewMember'),
(r'^members/(?P<member_number>[^/]+)/delete$', 'astonomyStuff.attendance.views.deleteMember'),
(r'^admin/add/talk/$', 'astonomyStuff.attendance.views.newTalk'),
(r'^talks/(?P<talk_title>[^/]+)/$', 'astonomyStuff.attendance.views.viewTalk'),
(r'^attendance/(?P<talk_title>[^/]+)/$', 'astonomyStuff.attendance.views.viewAttendance'),
(r'^databrowse/(.*)', databrowse.site.root),
(r'^adminDoc/doc/', include('django.contrib.admindocs.urls')),
(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
(r'^accounts/profile/$', 'astonomyStuff.attendance.views.adminPage'),
(r'^admin/add/attendance/$', 'astonomyStuff.attendance.views.addAttendance'),
(r'^members/(?P<member_number>[^/]+)/edit$', 'astonomyStuff.attendance.views.editMember'),
(r'^public/talks/$', 'astonomyStuff.attendance.views.publicViewTalks')
I have rearranged the order to see if this was the problem but that's not fixed it. Must the django admin application run on /admin/?
Edit
I have had the admin application working before just to let you know it only broke when I played around with the urls.
Edit 2
Here is my complete urls.py:
from django.conf.urls.defaults import *
from astonomyStuff.attendance.models import Member
from astonomyStuff.attendance.models import Non_Member
from astonomyStuff.attendance.models import Talk
from astonomyStuff.attendance.models import Event_Attendance
from django.contrib import admin
from django.contrib import databrowse
admin.autodiscover()
admin.site.register(Member)
admin.site.register(Non_Member)
admin.site.register(Talk)
admin.site.register(Event_Attendance)
databrowse.site.register(Member)
databrowse.site.register(Non_Member)
databrowse.site.register(Talk)
databrowse.site.register(Event_Attendance)
urlpatterns = patterns('',
# Example:
# (r'^astonomyStuff/', include('astonomyStuff.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# (r'^admin/add/member/$', 'astonomyStuff.attendance.views.newMember'),
# (r'^admin/add/$', 'astonomyStuff.attendance.views.addPage'),
# (r'^admin/$', 'astonomyStuff.attendance.views.adminPage'),
(r'^admin/$', include(admin.site.urls)),
(r'^talks/$', 'astonomyStuff.attendance.views.talksIndex'),
(r'^talks/past/$', 'astonomyStuff.attendance.views.viewAllTalks'),
(r'^members/$', 'astonomyStuff.attendance.views.viewMembers'),
(r'^members/(?P<member_number>[^/]+)/$', 'astonomyStuff.attendance.views.viewMember'),
(r'^members/(?P<member_number>[^/]+)/delete$', 'astonomyStuff.attendance.views.deleteMember'),
# (r'^admin/add/talk/$', 'astonomyStuff.attendance.views.newTalk'),
(r'^talks/(?P<talk_title>[^/]+)/$', 'astonomyStuff.attendance.views.viewTalk'),
(r'^attendance/(?P<talk_title>[^/]+)/$', 'astonomyStuff.attendance.views.viewAttendance'),
(r'^databrowse/(.*)', databrowse.site.root),
(r'^adminDoc/doc/', include('django.contrib.admindocs.urls')),
(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
(r'^accounts/profile/$', 'astonomyStuff.attendance.views.adminPage'),
# (r'^admin/add/attendance/$', 'astonomyStuff.attendance.views.addAttendance'),
(r'^members/(?P<member_number>[^/]+)/edit$', 'astonomyStuff.attendance.views.editMember'),
(r'^public/talks/$', 'astonomyStuff.attendance.views.publicViewTalks'),
)
Not this (r'^adminDJ/$', include(admin.site.urls)),
But this (r'^adminDJ/', include(admin.site.urls)), ##note, no $ in the regex
Remember folks, gotta check your regexes...
This is probably nothing but it caught my eye. From the first snippet:
(r'^admin/$', 'astonomyStuff.attendance.views.adminPage'),
(r'^adminDJ/$', include(admin.site.urls)),
And the second snippet:
(r'^admin/$', include(admin.site.urls)),
Is this how you want it? The first snippet uses ^adminDJ/$ while the second one uses admin/$ opposite include(admin.site.urls).
Are these two files? And are the both used? In that case the first snippet could very well override the second, thereby causing your admin views to show up instead of Django's.