Django-tagging: Grabbing objects by specifying multiple tags? - django

I currently have an entry in urls.py which fetches lone permalinks for my bugs:
from django.conf.urls.defaults import *
from tagging.views import tagged_object_list
from bugs.models import Bug
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^workarounds/', include('workarounds.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')),
(r'^$', 'django.views.generic.simple.direct_to_template', {'template':'homepage.html'}),
(r'^bugs/(?P<slug>[-\w]+)/$', 'bugs.views.bug_detail'),
(r'^bugs/tagged/(?P<tag>[^/]+)/$',
'tagging.views.tagged_object_list',
{
'queryset_or_model': Bug,
'template_name' : 'tag/lone.html'}),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
So if I specified a url of say, bugs/tagged/firefox it would bring up firefox tags. How could I make it filter out by multiple tags? eg: firefox+css would return all objects tagged with firefox and css.

You'll have to build your own view instead of using tagging.views.tagged_object_list.
(r'^bugs/tagged/(?P<tags>[-\w+]+)/$', your_tag_view)
In your view, get a list of the tags you are searching for:
tags = tags.split('+')
Then, use the TaggedItem.objects.get_by_model query, which conveniently will accept a list of tags:
from tagging.models import TaggedItem
bugs = TaggedItem.objects.get_by_model(Bug, tags)

Related

Adding simple views in 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)),
)

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

Regular expression issue when using Generic Views in Django

I am a front-end developer currently dabbling into Django to build a simple portfolio site to display my work. I used Django quite regularly in my previous job but its now been over 6 months and unfortunately I do have old work to look over.
My site consists of:
A homepage with links to featured "projects"
A project list page
A project detail page
I am trying to take advantage of Django's Generic Views to create the latter 2 pages for me. However I am encountering issues when editing my urls.py file. Here it is:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
from django.views.generic import list_detail
from homepage.models import Project
projects_list_info = {
'queryset' : Project.objects.all(),
'allow_empty': True,
'template_name' : '../templates/project_list.html',
}
projects_detail_info = {
'queryset' : Project.objects.all(),
'template_object_name' : 'project',
'slug' : 'slug',
'slug_field' : 'slug',
'template_name' : '../templates/project_detail.html',
}
urlpatterns = patterns('',
# Example:
# (r'^howelltocode/', include('howelltocode.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
(r'^test','homepage.views.viewTest'),
(r'^$','homepage.views.viewHome'),
(r'projects/$', list_detail.object_list, projects_list_info),
(r'^projects/(?P<slug>[-\w]+)/$', list_detail.object_detail, projects_detail_info),
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
The projects list view page successfully navigates to '/templates/project_list.html' when I type the url 'http://localhost:8000/projects'. However if I type 'http://localhost:8000/projects/htc-wildfire-facebook-application/' to view the "../templates/project_detail.html" page I get a 404 page with a "No project found matching your query" message.
I see this as encouraging as it obviously looking for a project but I think my regular expression could be wrong. Any help would be greatly appreciated.
Thanks
James
You've overwritten the slug coming from the URL with the bare value "slug" in your projects_detail_info dictionary. Remove that entry from the dict.

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.

Page Not Found error

Why I get this error?
Page not found (404)
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^admin/doc/
^admin/
The current URL, , didn't match any of these.
I just uncomment the related lines in urls.py to enable admin and admindoc. They both work; but when I go to the root domain (e.g. example.com), I get this error.
my urls.py is like this:
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^myproject/', include('myproject.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/', include(admin.site.urls)),
)
Thanks in advance...
That's because you don't have a URL defined for the root domain. Check our URLconf: it contains the two lines you listed, which means that you only have URLs defined for example.com/admin and example.com/admin/doc, but not just example.com.