Regular expression issue when using Generic Views in Django - regex

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.

Related

How can I fix my Wagtail URL namespace and explorer when adding wagtail to a current project?

My issue is my URL is coming up mysite.com/test-news-1/ instead of mysite.com/news/test-news-1/
I've started a new project using cookiecutter-django. I then wanted to add wagtail cms to the project and followed the docs to do so.
I get the wagtail admin page up fine, at /cms/ instead of /admin/ like it says to do on this page - http://docs.wagtail.io/en/v1.3.1/getting_started/integrating_into_django.html
I added a few apps just to practice and get used to wagtail, one directly copied from the wagtail blog example. This is where my issue starts.
The wagtail admin Explorer does not list my apps like it shows on the wagtail site https://wagtail.io/features/explorer/, instead it just says "Welcome to your new Wagtail site!" When I select Add Child Page it allows me to select the app pages I have set up and seems to go by my models just fine. But when I post something and click go to live site it comes up as mysite.com/blog1/ instead of mysite.com/blog/blog1/
I believe my problem that I dont understand the final part of the doc page that I linked above. It says,
Note that there’s one small difference when not using the Wagtail
project template: Wagtail creates an initial homepage of the basic
type Page, which does not include any content fields beyond the title.
You’ll probably want to replace this with your own HomePage class -
when you do so, ensure that you set up a site record (under Settings /
Sites in the Wagtail admin) to point to the new homepage.
I tried adding the homepage model from the doc page, but this didn't seem to help at all.
I'm very inexperienced, this is my urls.py file, if you need to see other files please let me know.
urls.py
from __future__ import unicode_literals
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic import TemplateView
from django.views import defaults as default_views
from wagtail.wagtailadmin import urls as wagtailadmin_urls
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailcore import urls as wagtail_urls
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'^about/$', TemplateView.as_view(template_name='pages/about.html'), name="about"),
# Django Admin, use {% url 'admin:index' %}
url(settings.ADMIN_URL, include(admin.site.urls)),
# User management
url(r'^users/', include("contestchampion.users.urls", namespace="users")),
url(r'^accounts/', include('allauth.urls')),
# Your stuff: custom urls includes go here
# Wagtail cms
url(r'^cms/', include(wagtailadmin_urls)),
url(r'^documents/', include(wagtaildocs_urls)),
url(r'', include(wagtail_urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
# This allows the error pages to be debugged during development, just visit
# these url in browser to see how these error pages look like.
urlpatterns += [
url(r'^400/$', default_views.bad_request, kwargs={'exception': Exception("Bad Request!")}),
url(r'^403/$', default_views.permission_denied, kwargs={'exception': Exception("Permission Denied")}),
url(r'^404/$', default_views.page_not_found, kwargs={'exception': Exception("Page not Found")}),
url(r'^500/$', default_views.server_error),
]
These two url confs are in conflict =
url(r'^$', TemplateView.as_view(template_name='pages/home.html'), name="home"),
url(r'', include(wagtail_urls)),
One must change, otherwise Django will always resolve the base url of `yourdomain.com/' to the first entry. One easy way to fix this is to update the second-
url(r'^content/', include(wagtail_urls)),
Now your Wagtail root page will be accessible at yourdomain.com/content.
As for mysite.com/blog/blog1/ not coming up, that Url would assume that, from your Wagtail root page, there's a page w/ slug 'blog', and then a child page of that with slug blog1. The tree structure of your Wagtail site determines the URLs by default. If you want to override that you'll have to use the RoutablePageMixin as described here.

Django admin panel works but main page gives me a 404

I'm brand new to Django and Python. I've only done php.
It took me three tries, but I've almost Django installed correctly. I followed the tutorial on installing it, and it worked. So I enabled the admin, panel, and set up mysql.
The admin panel works, and I've got my static folder so it has all of the css as well. But when I go to the main page I get this 404. (My project is called firstweb) So whenI go into firstweb there is a 404, but firstweb/admin works.
Using the URLconf defined in firstweb.urls, Django tried these URL patterns, in this order:
1. ^admin/
The current URL, firstweb/, didn't match any of these.
And I'm confused by the whole runserver thing. Is that some other little server other than my Apache? Why do I need to start it every time?
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('',
# Examples:
# url(r'^$', 'firstweb.views.home', name='home'),
# url(r'^firstweb/', include('firstweb.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)),
)
It looks like your url conf does not include any url pattern for any other pages accept the admin.
If you want the root url to display a page, uncomment this line
url(r'^$', 'firstweb.views.home', name='home'),
And then make sure that in firstweb/views.py you have a view function called 'home', which I assume is part of the tutorial.

Django: Issue with url conf

I'm trying to change my project to use class based views, and I'm running into a strange error with my url conf
I have a classed based view:
class GroupOrTeamCreate(AjaxableResponseMixin, CreateView):
model = GroupOrTeam
fields = ['name', 'description']
#success_url = reverse('home_page') # redirect to self
I have the last line commented out because if I don't, django complains that there are no patterns in my url conf.
To start, here's my base urls.py
urlpatterns = patterns('',
url(r'^$', TemplateView.as_view(template_name='core/home.html'), name='home_page'),
url(r'^administration/', include('administration.urls', app_name='administration')),
url(r'^reports/', include('reports.urls', app_name='reports')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Clearly there are patterns in there. If I comment out the administration urls, it works. So I assume the problem is in there somewhere.
administration urls.py
from django.conf.urls import patterns, url
from .views import ActiveTabTemplateView, GroupOrTeamCreate, GroupOrTeamUpdate
urlpatterns = patterns('',
# Add page
url(r'^add/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'), name='add_page'),
url(r'^add/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/add.html'),
name='add_page'),
# Seach page
url(r'^search/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'), name='search_page'),
url(r'^search/(?P<active_tab>\w+)/$', ActiveTabTemplateView.as_view(template_name='administration/search.html'),
name='search_page'),
#--------------------------------------------------------------------------
# Forms
#--------------------------------------------------------------------------
# Groups and teams
url(r'^group-or-team-form/$', GroupOrTeamCreate.as_view(template_name='administration/forms/groups_form.html'),
name='group_or_team_form'),
url(r'^group-or-team-form/(?P<pk>\d+)/$',
GroupOrTeamUpdate.as_view(template_name='administration/forms/groups_form.html'),
name='group_or_team_form'),
)
I'm not seeing the problem.. these pages load just fine without that reverse statement in, it appears to be the introduction of the reverse statement that breaks everything but I can't for the life of me work out what the cause is.
You get the error because the URL conf has not been loaded yet when the class is defined.
Use reverse_lazy instead of reverse.
from django.core.urlresolvers import reverse_lazy
success_url = reverse_lazy('home_page')

Create 404 with Django 1.4

I would like to show an 404 error if there's no proper entry in project_name/urls.py.
I read Writing your first Django app but i don't understood how to create a general error - not only for an app.
So i tried this:
TEMPLATE_DIRS = /var/www/first/templates
echo "404 ERROR" > /var/www/first/templates/404.html
in /var/www/first/urls.py:
from django.http import Http404
try:
...
url(r'^admin/', include(admin.site.urls)),
...
except:
raise Http404
After all i still get a 500 error if i call an unknown url.
Django uses the urls.py file to evaluate all the url patterns by order. If you want it to go to a certain page after it evaluated all the regex patterns and found no match, just add something like this as the last item in the urlpatterns tuple:
url('', views.defaultview),
with defaultview possibly being that 404 page.
django.views.defaults.page_not_found is a built-in django view that renders whatever's in templates/404.html.
It works with following in /var/www/first/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)),
...,
)

Django-tagging: Grabbing objects by specifying multiple tags?

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)