Update: Using Django 1.2.1 and Python 2.5.2 as offered by Dreamhost.
I'm having issues with the last part of the Django tutorial where the urls.py is changed to use generic views. After I change the code I get 404's on the pages and even the index stops working.
I have gone over all of my templates to see if that was the issue but I removed any instance of poll and replaced it with object. I have also attached the template for the index/object_list.
I am running this on Dreamhost and the static urls I set with views worked fine.
urls.py
from brsplash.models import Poll
from django.conf.urls.defaults import *
from django.contrib import admin
from django.views.generic import *
admin.autodiscover()
info_dict = {
'queryset': Poll.objects.all(),
}
urlpatterns = patterns('',
(r'^$', 'django.views.generic.list_detail.object_list', info_dict),
(r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
url(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='brsplash/results.html'), 'poll_results'),
(r'^(?P<poll_id>\d+)/vote/$', 'brsplash.views.vote'),
)
urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),
poll_list.html
{% if object_list %}
<ul>
{% for object in object_list %}
<li>{{ object.question }}</li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available</p>
{% endif %}
Django 1.3 introduced class-based generic views which will replace this function-based approach (see the note at the top of the documentation page) so perhaps it's best to use them instead.
With the class-based approach, your new detail-page url would look something like this:
from brsplash.models import Poll
...
from django.views.generic import ListView
urlpatterns = {'',
url(r'^$', ListView.as_view(model=Poll)),
...
}
This approach can be found in part 4 of the tutorial.
N.B.: I tend not to pass the template_name argument to as_view because, as stated in the docs:
ListView generic view uses a default template called <app name>/<model name>_list.html
You can upgrade to Django 1.3 on Dreamhost: blog.oscarcp.com/?p=167 –
jturnbull Sep 22 at 9:54
This fixed my issue with the urls.py issue I was having. Once I upgraded to 1.3.1 and changed the code to reflect it, my pages came back.
Related
I would like to incorporate an email form with Google Recaptcha similar or identical to this:
https://github.com/maru/django-contact-form-recaptcha
Into this existing django github project:
https://github.com/justdjango/video-membership
Where a website visitor could send emails to a Gmail account that I own directly from the form.
I edited the code from the video membership github project to include a contact page for this purpose.
How can this be accomplished?
video-membership-master/courses/urls.py:
from django.urls import path
from .views import ContactPageView
app_name = 'courses'
urlpatterns = [
path('contact/', ContactPageView.as_view(), name='contact')
]
video-membership-master/courses/views.py
from django.views.generic import TemplateView
class ContactPageView(TemplateView):
template_name = 'contact.html'
video-membership-master/courses/templates/contact.html:
{% extends 'courses/base.html' %}
{% block content %}
<h1>Contact</h1>
{% endblock %}
Dajax is not working, I am not able to understand why. I am using Django 1.7
My ajax.py file looks this:
from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register
#dajaxice_register
def jmc_foundation_tower_number(request, option):
print("It works!")
My template call is as follows:
<div class='col-lg-3'>
<select id='id_tower_number' name='tower_number' onchange="Dajaxice.core.views.jmc_foundation_tower_number(Dajax.process, {'option':$this.value})" onclick="Dajaxice.core.views.jmc_foundation_tower_number(Dajax.process, {'option':$this.value})" class='form-control'>
{% for tower in towers %}
<option value='{{ tower }}'>{{ tower }}</option>
{% endfor %}
</select>
</div>
My urls.py is as follows:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from dajaxice.core import dajaxice_autodiscover, dajaxice_config
dajaxice_autodiscover()
urlpatterns = patterns('',
url(r'^index$', 'core.views.index', name='index'),
url(r'^admin/', include(admin.site.urls)),
url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),
)
django-dajax and django-dajaxice
Should I use django-dajax or django-dajaxice?
In a word, No. I created these projects 4 years ago as a cool tool in
order to solve one specific problems I had at that time.
These days using these projects is a bad idea.
(...)
If you want to use this project, you are probably wrong. You should
stop couplig your interface with your backend or... in the long term
it will explode in your face.
jorgebastida/django-dajax
Apparently javascript function names with underscores('_') don't work when using functions like onclick.It's better to use functions like somefunction() instead of some_function() to make Dajax work.
The following is an example to make Helloworld using dajax
models.py:
from django.db import models
from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register
#dajaxice_register
def say_hello(request,value):
dajax = Dajax()
dajax.alert(value)
return dajax.json()
urls.py:
urlpatterns = patterns('',
# Examples:
url(r'^$', 'server.views.index', name='index'),
where "server" in server.views.index is the application name inside your project
in index.html file you have to use a jquery function to call this dajax request as following
index.html:
<html>
<script>
function print_helloworld(){
var value = Dajaxice.server.models.say_hello(Dajax.process,{'value':'Hello World!'});
}
</script>
<body>
<button id='mybtn' class='btn btn-primary' onclick='print_helloworld()'>Hello World</button>
</body>
</html>
if you need to access the index file from public ip you have to add the {% csrf_token %} token before button
I'm trying to complete a Django tutorial (https://docs.djangoproject.com/en/dev/intro/tutorial03/) and have run into a problem which, I think, is my understanding of regular expressions and the include() function.
My problematic 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'^polls/', include('polls.urls')),
)
which, leads me (upon browsing to http://localhost:8000/admin/) to the detail view of polls.urls - the code of which is:
from django.conf.urls import patterns, url
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
I get why it's matching to polls and getting taken through to polls.urls. Any ideas?
Note: when I comment out the url(r'^polls/, include('polls.urls')), line I can see the admin page again
Thanks!
O.K - so I managed to fix this but am not entirely sure how. It was a case of leaving it and working on other problems and when I came back it was fixed! So, things that I changed:
1.) included {% load url from future %} to ensure forward compatibility in my templates
2.) changed the form action in detail.html to <form action="/polls/{{ poll.id }}/vote/" method="post"> (from <form action="{% url 'polls.views.vote' poll.id %}" method="post">
3.) finally, and probably most importantly - sorted my syntax out! In my views.py file I had Return HttpResponseRedirect(reverse('polls_results', args=(p.id,))) and not Return HttpResponseRedirect(reverse('poll_results', args=(p.id,)))
I still don't know which did it but, if anybody is in the same position as me, some combination of those 3 will do it!
I have the following setup:
/landing_pages
views.py
urls.py
In urls.py I have the following which works when I try to access /competition:
from django.conf.urls.defaults import *
from django.conf import settings
from django.views.generic.simple import direct_to_template
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^competition$', 'landing_pages.views.page', {'page_name': 'competition'}, name="competition_landing"),
)
My views.py has something like this:
def page(request, page_name):
return HttpResponse('ok')
Then in a template I'm trying to do this:
{% load url from future %}
<a href="{% url 'landing_pages.views.page' page_name='competition'%}">
Competition
</a>
Which I apparently can't do:
Caught NoReverseMatch while rendering: Reverse for 'landing_pages.views.page' with arguments '()' and keyword arguments '{'page_name': u'competition'}' not found.
What am I doing wrong?
You ask in your comment to DrTyrsa why you can't use args or kwargs. Just think about it for a moment. The {% url %} tag outputs - as the name implies - an actual URL which the user can click on. But you've provided no space in the URL pattern for the arguments. Where would they go? What would the URL look like? How would it work?
If you want to allow the user to specify arguments to your view, you have to provide a URL pattern with a space for those arguments to go.
{% url [project_name].landing_pages.views.page page_name='competition' %}
Or better
{% url competition_landing 'competition' %}
simply put mentions of reverse() anywhere in my project were failing, and so was {% url %}.
I have since made some progress if you scroll to the bottom!
relevant files
root/urls.py
from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.views import serve as serveStatic
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^dbrowse/', include('dbrowse.urls')),
(r'^static/', serveStatic),
url(r'^$', 'core.views.viewHallo',name='home'),
)
root/core/views.py
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from site import site_store
def viewHallo (request):
pass
return render_to_response ('core.html',
{'site':site_store,
'title':'i am the hallo view',
'content':'Hallo World!',},
context_instance=RequestContext(request))
Notes
I first noticed the reverse() failing when i had a file called site.py in my project that tried to call reverse(). I was using it to store site settings. I was using the file because
I didn't want to use the bother the database with data that would rarely change.
If I nuked my projects database I didn't want my site settings also going down
I have since found a way to use models to achieve the two goals.
but all that is just extra background info, in case you here someone commenting about a site.py.
update 25/02/11
well here goes!
first notice that urls.py has (r'^dbrowse/', include('dbrowse.urls')). that caused reverse() to fail. I'll explain later...
as for the template tag, I've discovered that the {% url %} doesnt take variables. I took this completely for granted.In fact when I was testing the template tag, i'd just go in and hard code something such as {% url 'home' %} which would work and sometimes i'd test {% url home %} with home being a variable. I din't even see this as being completely different test cases.
But i now know {% load url from future %} allows you to use variables as arguments to {% url %}
Anyway, now back to the (r'^dbrowse/', include('dbrowse.urls')) in urls.py
I had a folder like so
project\
--dbrowse\
__init__.py
urls.py
now this is dbrowse/urls.py
from django.conf.urls.defaults import patterns, url
#databrowse
from django.contrib import databrowse
databrowse.site.register(MyModel)
urlpatterns = patterns('',
url(r'(.*)',databrowse.site.root, name='dbrowse'),)
this was my attempt to avoid having to put databrowse.site.register(MyModel) in my project's root urls.py like the docs suggested. I dont like the idea of polluting my projects main urls.py with databrowse.site.register(MyModel)
however I still dont understand why this caused reverse() to break. but i'm supecting it's to do with (.*) being in the pattern.