Edit your "project/urls.py" to include your app "urls.py".
urlpatterns = patterns('',
# Some default url patterns here...
(r'^example/', include('yourapp.urls')),
)
Now thats done, you can safely customise any urls in your app without affecting anything else in the project.
Edit "project/yourapp/urls.py" and put in the following.
from django.conf.urls.defaults import patterns, url
urlpatterns = patterns('',
url(r'^$', 'yourapp.views.home'),
)
The first case is simple, you simply point your browser to http://yourdomain.com/example and it'll show you the output of 'yourapp.views.home'.
Now, what is that supposed to be? "urls.py" maps the URL request to a view and a view is implemented in your app "views.py" file. This view handles the URL requests with the behaviour and logic you provide it.
Edit "project/yourapp/views.py" and put in the following text.
from django.shortcuts import render_to_response
def home(request):
return render_to_response("yourapp/home.html")
See the tutorial on how to create Django templates in order to understand what "home.html" contain, but for now just create "youapp/templates/yourapp/home.html" and throw in some random text in there.
Open up http://yourdomain.com/example in the browser and your random text should appear. Horray! You have just successfully mapped the first url to a view.
The next crumb
The second url handler will resolve the http://yourdomain/example/test request by mapping "test" to test_view().
And add a new url mapping "url(r'^test$', 'yourapp.views.test_view)" to the "urlpatterns" in your app "urls.py" file.
Create a method called test_view() in your "views.py" file.
def test_view(request):
return render_to_response("yourapp/home.html")
At this point you can reuse your "home.html" or create a new template file. Try it, it might make you happy =)
Input arguments from URL
The last url "url(r'^number/(?P<input_integer>\d+)/$', 'yourapp.views.example_with_input')" is a bit trickier. It maps http://yourdomain/example/number/345 (or any number in place of 345) to example_with_input().
def example_with_input(request, input_integer):print input_integer
return render_to_response("yourapp/input_test.html", { 'number': input_integer })
Make sure that the named callback "input_integer" in the regular expression is the same as the argument in example_with_input() function.
The "print" line however isnt necessary, but it'll show the input number into the console where Django is running.
Now in the same folder as your "home.html" template, add "input_test.html". Edit it and fill in the following.
{{ number }}
That should now spit out whatever number you put into the url.