Django dummie question - django

Hi i'm beginning to learn django and i'm facing same trouble .Could you guys help with a silly question .
i have a nav menu at my base.html it works fine but the problem is that when i click in one of the links , my view.py render_to_response correctly to the page , but once i got there if i click again in this link it just and to my url the same pattern and my urls.py dont find it.
Same thing like that.
First time click :
//127.0.0.1:8000/cliente/cadastro/
Second time click:
//127.0.0.1:8000/cliente/cadastro/cliente/cadastro/
I belive that is the same problem i'm facing with admin
First time click :
//127.0.0.1:8000/admin/
It works.
Second time click:
//127.0.0.1:8000/cliente/cadastro/admin/
it does not Work.
The url.py is the follow:
urlpatterns = patterns('',
# Example:
(r'^', include('cliente.urls')),
(r'^admin/', include(admin.site.urls)),
)
#seta os arquivos estaticos , css e javascript
urlpatterns += patterns('',
(r'^/css/(?P<path>.*)$', 'django.views.static.serve',
{'document_root':'/home/lioy/django_projects/terrasis/css/'}),
(r'^/js/(?P<path>.*)$', 'django.views.static.serve',
{'document_root':'/home/lioy/django_projects/terrasis/js/'}),
)
the include('cliente.urls') :
urlpatterns = patterns('cliente.views',
(r'^$', 'home'),
(r'^cliente/cadastro/$', 'cadastro'),
)
how can i managed that ?

In your base.html file you should make sure to use absolute paths instead of relative.
This is an example of an absolute path (note the leading forward-slash):
A client
<!-- ^ leading slash -->
And this is what you should probably avoid:
This is an example of a relative path (no leading forward-slash):
ay, que barbaridad
<!-- ^ no leading slash -->

Related

Django: Changing the index site? (home page)

The index site on my Django homepage stopped working because of a problem that will take a very long time to fix. The site can't be down for that long so I am trying to change the index site so that if you go to the primary url you will atleast end up on the website.
What I have done is change the urls.py file in the primary application, where I simply replaced the line
url(r'^', include('news.urls', namespace='news')),
to
url(r'^', include('events.urls', namespace='events')),
in the urlpatterns list, where news is the faulty page and events is the page that I want to be shown. However, after pushing this to live nothing changed, and for some reason my local Django development server is not working.
Did I do anything wrong, or is there anything else I have to do as well?
Thanks.
In the events app make sure you have a URL such as
......
url(r'^$', views.EventsIndex.as_view(), name='index'),
......
Replacing news.urls with events.urls may cause problems, if there are views/templates that try to reverse news urls. It would be better to leave the include as it is, and add a new URL pattern above that for the index:
from events.views import home
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^', include('news.urls', namespace='news')),
...
]

How to expand error in Django dev server?

I have trouble figuring out what trailing slash that causing an error in Django. This is the error:
?: (urls.W002) Your URL pattern '^/' has a regex beginning with a '/'.
Remove this slash as it is unnecessary.
My urls.py looks like this:
urlpatterns += [
#url('^$', direct_to_template, {'template': 'index.html'}, name='home'),
url('^$', mezzanine.blog.views.blog_post_list, name='home'),
url('^', include('main.urls')),
url('^', include('mezzanine.urls')),
]
And in my main.urls it doesn't exist any '/' before. How can I troubleshoot where the problem exists? Can I expand the error when I run the dev server with manage.py?
Edit:
This is my main/urls.py:
from django.conf.urls import patterns, include, url
from . import views
app_name = 'main'
urlpatterns = [
# Create Blog
url(r'^create-blog/$', views.create_blog, name='create_blog'),
# Lists
url(r'^blogs/$', views.blog_list, name='blog_list'),
url(r'^blogs/new/$', views.blogs_new, name='blogs_new'),
url(r'^top/$', views.top_lists, name='top_lists'),
url(r'^latest/$', views.latest_posts, name='latest_posts'),
# Tags
url(r'^tags/(?P<tag>[\w-]+)/$', views.tag, name='tag'),
url(r'^tags/$', views.tags, name='tags'),
# Avatars
url(r'^avatar/', include('avatar.urls')),
# Various
url(r'^welcome/$', views.welcome, name='welcome'),
url(r'^faq/$', views.faq, name='faq'),
url(r'^contact-us/$', views.contact, name='contact'),
]
Looking over the question again, perhaps you have literally defined a pattern as ^/ somewhere.
?: (urls.W002) Your URL pattern '^/' has a regex beginning with a '/'. Remove this slash as it is unnecessary.
This system check by django is printing out the pattern that is causing problems. I made a quick django example, and when I put in a url such as...
urlpatterns = [
url('^test1/$', views.test1, name='test1'),
url('^/test2/$', views.test2, name='test2'),
]
?: (urls.W002) Your URL pattern '^/test2/$' [name='test2'] has a regex beginning with a '/'. Remove this slash as it is unnecessary.
As you can see it is giving me my URL pattern exactly as it is defined, ^/test2/$. I think your problem is likely in some other part of your urls.py file or one of the other urls.py files, most likely an include because you do not have the trailing $ in the pattern nor a name for the view.
Try searching through your project for a url defined as '^/' is my recommendation.

Django index url confusion

Hi thanks for looking into this.
I have been following Django's tutorial on URLs and got a bit confused/stuck on this part:
https://docs.djangoproject.com/en/1.4/intro/tutorial03/#decoupling-the-urlconfs
what I do not understand is if, say, on page mypage.com I provide only 2 possible URLs for mypage.com/polls and mypage.com/admin, what happens if the user goes to mypage.com? Obviously, I thought, the user will need to see some sort of 'welcome' page so I decided to add another URL to that urls.py:
urlpatterns = patterns('',
url(r'^/', 'myapp.views.welcome'), #when it's just mysite.com
url(r'^myapp/', include('myapp.urls')), #includes everything with mysite.com/myapp/...
url(r'^admin/', include(admin.site.urls)),
)
But then I get redirected to that welcome view from whichever page, whether i go to /myapp or not. So, I decided to create a views.py file outside myapps folder and put that welcome page there, and it seems to have worked, apart from that I get a 404.
I am so confused! Could you explain in lamers' terms please
Thanks,
blargie-bla
It should be
url(r'^$', 'myapp.views.welcome')
otherwise any URL will match the pattern. Django will call the view for the first pattern in urlpatterns that matches, so you need to be specific and include the end-of-the-line character ($) into the pattern.

Django "Page not found" error page shows only one of two expected urls

I'm working with Django, admittedly for the first time doing anything real.
The URL config looks like the following:
urlpatterns = patterns('my_site.core_prototype.views',
(r'^newpost/$', 'newPost'),
(r'^$', 'NewPostAndDisplayList'), # capture nothing...
#more here... - perhaps the perma-links?
)
This is in an app's url.py which is loaded from the project's url.py via:
urlpatterns = patterns('',
# only app for now.
(r'^$', include('my_site.core_prototype.urls')),
)
The problem is, when I receive a 404 attempting to utilize newpost, the error page only shows the ^$ -- it seems to ignore the newpost pattern...
I'm sure the solution is probably stupid-simple but right now I'm missing it. Can someone help get me on the right track...
Your pattern for the include will only match an empty URL string, change it to a prefix which should be mapped to the included urls, or remove the $ from that pattern.

Django admin fails when using includes in urlpatterns

I am trying to refactor out my application a little bit to keep it from getting too unwieldily. So I started to move some of the urlpatterns out to sub files as the documentation proposes.
Besides that fact that it just doesn't seem to be working (the items are not being rerouted) but when I go to the admin, it says that 'urlpatterns has not been defined'.
The urls.py I have at the root of my application is:
if settings.ENABLE_SSL:
urlpatterns = patterns('',
(r'^checkout/orderform/onepage/(\w*)/$','checkout.views.one_page_orderform',{'SSL':True},'commerce.checkout.views.single_product_orderform'),
)
else:
urlpatterns = patterns('',
(r'^checkout/orderform/onepage/(\w*)/$','commerce.checkout.views.single_product_orderform'),
)
urlpatterns+= patterns('',
(r'^$', 'alchemysites.views.route_to_home'),
(r'^%s/' % settings.DAJAXICE_MEDIA_PREFIX, include('dajaxice.urls')),
(r'^/checkout/', include('commerce.urls')),
(r'^/offers',include('commerce.urls')),
(r'^/order/',include('commerce.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^accounts/login/$', login),
(r'^accounts/logout/$', logout),
(r'^(?P<path>.*)/$','alchemysites.views.get_path'),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.MEDIA_ROOT}),
The urls I have moved out so far are the checkout/offers/order which are all subapps of 'commerce' where the urls.py for the apps are so to be clear.
/urls.py in questions (included here)
/commerce/urls.py where the urls.py I want to include is:
order_info = {
'queryset': Order.objects.all(),
}
urlpatterns+= patterns('',
(r'^offers/$','offers.views.start_offers'),
(r'^offers/([a-zA-Z0-9-]*)/order/(\d*)/add/([a-zA-Z0-9-]*)/(\w*)/next/([a-zA-Z0-9-)/$','offers.views.show_offer'),
(r'^reports/orders/$', list_detail.object_list,order_info),
)
and the applications offers lies under commerce.
And so the additional problem is that admin will not work at all, so I'm thinking because I killed it somewhere with my includes.
Things I have checked for:
Is the urlpatterns variable accidentally getting reset somewhere (i.e. urlpatterns = patterns, instead of urlpatterns+= patterns)
Are the patterns in commerce.urls valid (yes, when moved back to root they work).
So from there I am stumped. I can move everything back into the root, but was trying to get a little decoupled, not just for theoretical reason but for some short terms ones.
Lastly if I enter www.domainname/checkout/orderform/onepage/xxxjsd I get the correct page. However, entering www.domainname/checkout/ gets handled by the alchemysites.views.get_path.
If not the answer (because this is pretty darn specific), then is there a good way for troubleshoot urls.py? It seems to just be trial and error. Seems there should be some sort of parser that will tell you what your urlpatterns will do.
Adding the following line in my urls.py worked for me:
from django.conf.urls import include
Have a look at the django docs for including other url confs. I think you might have misunderstood them. In particular
Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
As an example, you have
(r'^/checkout/', include('commerce.urls')),
(r'^/offers',include('commerce.urls')),
(r'^/order/',include('commerce.urls')),
This means that
/checkout/offers/
/offers/offers/
/order/offers/
will all match the url pattern (r'^offers/$','offers.views.start_offers') in commerce/urls.py.
If you want to define a view for /checkout/ in commerce.py, you need to add the pattern
(r'^$', 'path_to_your_view')
because the /checkout/ part will be chopped off by the include()
As, an aside:
In /commerce/urls.py, use
urlpatterns = patterns('',
...
for the first patterns you define. You can then use urlpatterns += later in the same file.