I am pretty new to django but experienced in Python and java web programming with different frameworks. I have made myself a nice little django app, but I cant seem to make it match www.mysite.com as opposed to www.mysite.com/myapp.
I have defined urls and views in my urls.conf which is currently not decoupled from the app (don't mind that).
urlpatterns = patterns('myapp.views', (r'^myapp/$', 'index'), (r'^myapp/(?P<some_id>\d+)/global_stats/$', 'global_stats'), (r'^myapp/(?P<some_id>\d+)/player/(?P<player_id>\d+)/$', 'player_stats'), )
All this works like a charm. If someone goes to www.mysite.com/myapp they will hit my index view which causes a http redirect to my "correct" default url.
So, how can I add a pattern that will do the same as (r'^myapp/$', 'index') but without the /myapp--that is, www.mysite.com should suffice?
I would think this would be very basic stuff... I tried adding a line like:
(r'^$', 'index'),
however this throws me in a loop...
Hope you django gurus out there can clarify this for me!
Now, start the server and enter localhost:8000/hello to the browser. This URL will be mapped into the list of URLs and then call the corresponding function from the views file. In this example, hello will be mapped and call hello function from the views file. It is called URL mapping.
A quick way is to include the projects url patterns at the new path. At the bottom of the main urls.py include the base_patterns at the new path.
Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .
URL routing flow As we can see, the user enters a URL in the search bar of the web browser. Then, that URL is passed to the url.py file where it tries to find a matching URL pattern. If a matching URL is found, the corresponding view function attached with that URL is invoked. Otherwise, it returns 404 .
I know that this question was asked 2 years ago, but I've faced the same problem and found a solution:
In the project urls.py
:
urlpatterns = patterns('', url(r'^', include('my_app.urls')), #NOTE: without $ )
In my_app.urls.py
:
urlpatterns = patterns('', url(r'^$', 'my_app.views.home', name='home'), url(r'^v1/$', 'my_app.views.v1', name='name_1'), url(r'^v2/$', 'my_app.views.v2', name='name_2'), url(r'^v3/$', 'my_app.views.v3', name='name_3'), )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With