开发者

Where to put Python files to be redirected to by urls.py in Django?

开发者 https://www.devze.com 2022-12-20 03:26 出处:网络
Where do I put python files to be redirected to by urls.py in Django? The tutorial showed something like this:

Where do I put python files to be redirected to by urls.py in Django? The tutorial showed something like this:

urlpatterns = patterns('', (r'^polls/$', 'mysite.polls.views.index'),

Where do I set up pages to be easily linked as something.something.page like this? I am currently just trying to drop st开发者_运维问答raight .py files in random directories and typing the name of the file in the urls.py file like so:

urlpatterns = patterns('', (r'file', 'file.py'),

Which is obviously not the correct way to do it. How do I create pages to be linked to in urls.py? Thanks.


You need to use views. You can create views (keep reading the official django documentation), then import them into your urls.py file and use them. Here's an example:

views.py

from django.shortcuts import render_to_response

def index(request):
   """
   Main page.
   """
   return render_to_response('index.html') # display index.html

urls.py

from myproject.views import index
urlpatterns = patterns('',
   (r'^$', index),
)

This example will display your index.html page whenever you visit the root of your website (eg: /).

0

精彩评论

暂无评论...
验证码 换一张
取 消