How to setup Django default function? - django

I'm not sure that my question is correct.
I develop a web site on local machine and I can not understand how to setup a view which will be executed when I open a start page http://127.0.0.1:8000/
I've tried to write a url - url(r'^/', 'pages.views.home', name='home'), but when I go to browser it doesn't work.
Maybe I need to change settings.

Try using the following URL regex:
url(r'^$', 'pages.views.home', name='home')
The first / is not part of the regex because its automatically removed before comparison against the URLs. The above expression will match the first / and nothing more - ^ means 'beginning of the line' and $ means the end.

Related

Django URL explanation

I'm facing an issue in Django URLs which is when I direct to localhost:8000/dashboard or localhost:8000/dashboard1111111111, it opens the same page.
Although in my urls.py file I have only /dashboard
How do I throw an error page if URL is incorrect?
Add $ at the end of the regular expression:
url(r'^dashboard$', views.dashboard, name='dashboard'),
This would require the end of the string after the dashboard word.

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.

how can I classify apps in a django website

I wanna create a website using django framework , I don't know how to divide the website into several apps... Help me.
In advance , thanks a lot
by the way , I'm new to web development
Django works by matching a URL pattern to some code that you have written in views.py.
In your case, you are pointing the same pattern (^$) to two view methods. Django will stop when it finds a match, so when you switch the patterns around, it will always match the first entry in the list.
If you change your patterns to:
urlpatterns = patterns('',
url(r'^/two$', 'myapp2.views.home2', name='home2'),
url(r'^$', 'myapp1.views.home1', name='home1'),
Now when you type htt://localhost:8000/two home2 will be executed, and when you type http://localhost:8000/ home1 will be executed.

Django-CMS AppHooks with conflicting urls?

I'm trying to use django-cms app hooks in a different way. I have only an app, with different website pages. For each page, i created an AppHook, since i want to have control of all of them with the cms.
To do that, inside the app, i did a package, with urls.py file for each of the page, example:
/urls
/home_urls.py
/portfolio_urls.py
/contacts_urls.py
Here are the definition of some app hooks:
class WebsiteHome(CMSApp):
name = _("cms-home")
urls = ["website.urls.home_urls"]
apphook_pool.register(WebsiteHome)
class WebsiteServices(CMSApp):
name = _("cms-services")
urls = ["website.urls.services_urls"]
apphook_pool.register(WebsiteServices)
Anyway, the problem is: i don't have any control on the regular expressions. Each one, is entering on the first regular expression that it founds, in this case, the urlpattern in the
website.urls.home_urls
Despite, having different apphHooks.
Example:
if i write a slug contacts (that has an apphook to WebsiteContacts), it still goes to the home_urls.py file, associated with the WebsiteHome (app hook).
Did anyone had a similiar problem?
Basically, what I'm trying to say is that it's something wrong with the regular expression. I can't make:
url(r'^$', [...]),
only:
url(r'^', [...]),
If I put the '$', it doesn't enter on any regex. If I take it, it enters always on the
website.urls.home_urls.py
Despite the slugs having different Apphooks, associated with different urls.py files.
Have you tried r'^/$'? I'm using r'^/?$' in some app-hook urls, but I wonder if r'^$' is failing for you because of a '/'?
As you've defined each of those URL files as individual app hooks in CMS then they'll each get attached to a certain page in the CMS e.g.
www.mysite.com/home
www.mysite.com/contacts
www.mysite.com/services
etc
Because those URL files are attached to pages this should prevent conflict between urlpatterns. For example, I've got an URLs file attached to a CMS app called News which looks like this;
urlpatterns = patterns(
'',
url(r'^(?P<slug>[-_\w]+)/$', NewsDetailView.as_view(), name='news_detail'),
url(r'^$', NewsListView.as_view(), name='news_list'),
)
Which is attached to a page at mysite.com/news so if I go to mysite.com/news/myslug I hit that NewsDetailView and if I go to mysite.com/news I hit NewListView.
Using this example, if you had a slug for a contact you'd go to mysite.com/contacts/contact-slug to hit that NewsDetailView.
And just a sidenote on the urlpatterns in case you're not aware, the ^ in the regex signifies the start of a pattern to match, and the $ signifies the end. URL dispatcher docs

redirect rule for nginx?

I am running my django app using nginx. I want to write a redirect rule such that
if user hit the url http://example.com/django/nginx/ then it redirect it to
http://example.com/django/#!/nginx/. I want o know the regex for it.
Thanks
You'll want to handle this on the client side (through Javascript, most likely), not through nginx.
From what I understand, the point of # in URLs (as per the spec) is that the portion that comes after # doesn't reach the server.
Also, see this question for some info on JS libraries for working with hash-bang urls: Are there any javascript libraries for working with hashbang/shebang (#!) urls?
Given you example I'm assuming that you are working with URLs in the form "http://1/2/3/" only, so nothing going beyond 3. Where you want to separate 2 and 3 with "/#!/". If that is the case you can try the following.
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
('^django/(?P<ajax_section>\w+)/$', redirect_to, {'url': '/django/#!/%(ajax_section)s/'}),
)
The above assumes that 2("django") in the URL will be fixed. If that is not the case you will have to try and make it a parameter as well.