Django {% url %} behaving strangely - django

Can someone explain why this works:
<li>HOME</li>
<li>PROJECTS</li>
While this doesn't:
<li>HOME</li>
<li>PROJECTS</li>
I get a 500 error with ImportError at /index/ No module named project however I'm not trying to import project anywhere. (I am importing Project, which has been importing fine for days, and works fine when I don't use {% url %} style links). I've seen a number of answers here and blog posts elsewhere suggesting that {% url %} is a bad idea. I'm happy to stop using them if necessary, but for the life of me can't see where the error is coming from.
Contents of urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.views.static import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^index/', 'projects.views.index', name='index'),
url(r'^login/', 'project.views.login', name='login'),
url(r'^project/(?P<project_id>\d+)/$', 'projects.views.project', name='project'),
url(r'^project/', 'projects.views.project', name='project'),
url(r'^sched/(?P<project_id>\d+)/$', 'projects.views.schedule'),
url(r'^luminaires/(?P<project_id>\d+)/$', 'projects.views.luminaires'),
url(r'^luminaire/(?P<project_id>\d+)/(?P<luminaire_id>\d+)/$', 'projects.views.luminaire'),
url(r'^deleteproject/(?P<project_id>\d+)/$', 'projects.views.deleteproject', name = 'deleteproject'),
url(r'^admin/', include(admin.site.urls)),
# Examples:
# url(r'^$', 'relume.views.home', name='home'),
# url(r'^relume/', include('relume.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes':True}),
)

After looking at your urls.py file, it seems you have a typo in the login's line of urls where you have used project instead of projects.

Related

How can I add a href link to another page in DjangoCMS index page

I have a website developed using DjangoCMS and I tried to add a href to another page (site.com/en/blog). Unforunately none of my tries went well:
urls.py
from __future__ import print_function
from cms.sitemaps import CMSSitemap
from django.conf.urls import * # NOQA
from django.conf.urls.i18n import i18n_patterns
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)), # NOQA
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
{'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^select2/', include('django_select2.urls')),
url(r'^', include('cms.urls')),
url(r'^blog', include('cms.urls')),
)
# This is only needed when using runserver.
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', # NOQA
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + staticfiles_urlpatterns() + urlpatterns # NOQA
index.html
...
<li class="menu-item-has-children">Blog
<ul class="sub-menu">
<li>3 Columns</li>
<li>2 Columns</li>
<li>2 Columns with sidebar</li>
<li>Standard</li>
<li>Fullwidth</li>
<li>Modern</li>
<li>Modern 2</li>
<li>Modern 2 Lite</li>
</ul>
...
I've tried the following:
Blog or
Blog or
Blog
But every time I get 404-Page not found. I am sure that the page exists at that path.
Any hints, please ?
Instead of manually writing the menus yourself, you should use the {% show_menu %} template tag in django CMS.
If you need to have non-CMS menu nodes too, look at how to extend the menu in django CMS.

Need a seconda pair of eyes on Django URL dispatcher

Wondering if I could get some help with a second pair of eyes. Started working Django about two months ago learning in my spare time. Long story short: My index and post view work fine. I recently created a contact html\template and view which looks like it goes right back to my index page? Here is my project urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
from .settings import MEDIA_ROOT
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls')),
url(r'^contact/', include('blog.urls')),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
)
Here is my app urls.py
from django.conf.urls import patterns, include, url
from blog import views
urlpatterns = patterns('blog.views',
url(r'^(?P<post_name>\w+)/$', views.post, name='post'),
url(r'^contact/$', views.contact, name='contact'),
url(r'^$', views.index, name='index'),
)
If I change my contact view to come up as my default the view it comes up fine.
urlpatterns = patterns('blog.views',
url(r'^(?P<post_name>\w+)/$', views.post, name='post'),
url(r'^$', views.contact, name='contact'),
#url(r'^$', views.index, name='index'),
)
As soon as switch it back to the original state when I click on the contact link on my page it goes right to my index page. I have to be doing something wrong with the URL dispatcher part but I'm not sure what.
Your contact urlpattern is not matching the URL "/contact", it is matching "/blog/contact" and "/contact/contact". Since you've included the blogs urlpatterns under both those prefixes, the thing that matches "/contact" is the URL without anything after the prefix, ie the index.
You probably don't want to put the contact pattern into the included file at all: just match it directly in the base project file.
Please mind the order of the urls in your app's urls.py. Try to put your views.contact view before your views.post view. Keep in mind: Django chooses the first matching regular expression, which is your views.post view.
I ran into a similar problem and described the solution here.
Here your contact view has the url /contact/contact/ and index view has the url /contact/
So you have rewrite the project urls as
from django.conf.urls import patterns, include, url
from django.contrib import admin
from .settings import MEDIA_ROOT
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^blog/', include('blog.urls')),
url(r'^', include('blog.urls')),
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': MEDIA_ROOT}),
)

django tutorial 3 indexpage missing

i am learning django by using django 1.6 documents tutorial 1 - 6.
this round is my 4th try and, previous 3 try was successful and i understand more on every try.
i am in tutorial 3 now, to create views.
according to the documents, after i created a view, i need to map it to a URL.
so i follow the documents to add a urls.py in the polls directory.
and then i follow the document to add include() to mysite/urls.py
i am able to so called wired an index view into the polls view.
now if i go back to localhost:8000, i get an error page,
so my question is
1) WHY?
2) how to get back my localhost:8080 index page co-exist with the polls index page?
thank you very much everyone for your time. i am new and sorry for so simple question.
thank you.
updated:
this is my urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^polls/', include('polls.urls')),
)
this is my polls/urls.py
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
)
the error msg is:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
^polls/
The current URL, , didn't match any of these.
http://localhost:8000 index page will be displayed when you created a project (mysite). After creating an application and including it in installed apps in settings.py, you will no longer view the index page of the project, rather your admin page of the application (polls) will be visible at http://localhost:8000/admin
And if you had created any views with the following patterns in polls/urls.py
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^blog/$', views.blog, name='blog'),
)
You need to visit http://localhost:8000/polls for your index page and http://localhost:8000/polls/blogs for your poll blogs page
Editing my answer for step by step approach:
create index.html at polls/templates/polls directory.
in polls/views.py
from django.shortcuts import render_to_response
def pollindex(request):
return render_to_response('polls/index.html', context_instance=RequestContext(request))
in polls/urls.py
urlpatterns = patterns('',
url(r'^index/$', views.pollsindex, name='index'),
)
in your project/urls.py
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
And now you can view your template index.html at http://localhost:8000/polls/index

Django syntax error in urls.py

in my website's main urls.py file, I am trying to add this line:
url(r'^account/',include('accounts.urls', namespace='accounts')),
after adding the line, my file looks like this:
from django.conf.urls import patterns, include, url
from invest1.views import *
#to enable admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'invest1.views.home', name='home'),
url(r'^about/$', 'invest1.views.about', name='about'),
url(r'^contact/$', 'invest1.views.contact_us', name='contact'),
url(r'^account/', include('accounts.urls', namespace='accounts')),
url(r'^profile/', include('profiles.urls', namespace='profiles')),
url(r'^post/', include('posts.urls', namespace='posts')),
#uncomment to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
#to enable admin
url(r'^admin/', include(admin.site.urls)),
)
no matter what I do to the file, I get the error
SyntaxError at /
invalid syntax (urls.py, line 18)
with the accounts line commented out, the website runs. but no matter where I put the line, it always gives me the same error with the same line number. I've even retyped the whole thing to make sure it's not some illegal whitespace character. I feel like I'm missing something really trivial...what's causing the error?
The problem is probably in one of the other urls.py (e.g. accounts.urls).

Django cannot find admin.sites.url

I am developing a django site locally using rc1.3 and after making several adjustments to my urls.py file i am receiving error messages that django cannot import admin.site.urls. by urls.py file is below.
urlpatterns = patterns('',
# Example:
(r'^city/$', 'venue.views.city_index'),
(r'^accounts/', include('registration.urls')),
(r'^accountss/', include('registration.backends.simple.urls')),
(r'^profiles/', include('profiles.urls')),
(r'^admin/', include('admin.site.urls')),
(r'^city/(?P<city>[-\w]+)/$', 'venue.views.city_detail'),
(r'^city/(?P<city>[-\w]+)/venue/$', 'venue.views.venue_index'),
)
Any idea what might be cause this, thanks.
http://docs.djangoproject.com/en/dev/intro/tutorial02/#activate-the-admin-site
Don't include the string path, actually import the admin module and point to its urls.
from django.contrib import admin
urlpatterns += patterns('',
(r'^admin/', include(admin.site.urls)),
)