开发者

Django: Simplest way to create a 2-page site?

I have a site made in PHP that i need to create a django site of. I've stripped out the PHP code temporary (not much code anyways), but i'm having problems understanding how django works and how to create a simple template to display a page.

I know there's thousands of books and guides out there, but most of them go too deep or doesn't do what i need. I just need two simple pages, page1 and page2, which will be accessed through domain.com/page1 and domain.com/page2.

What is the simplest way to achieve that?

This is what i have in my urls.py file so far, is that correct at least?

from dja开发者_如何学运维ngo.conf.urls.defaults import patterns, include, url

urlpatterns = patterns('',
    url(r'^$', 'mysite.views.page1', name='home'),
    url(r'^$page2', 'mysite.views.page2', name='page2'),
)

It obviously doesn't work now cause the views aren't created.

Any help is greatly appreciated, // qwerty


I recommend you to walk through tutorial, you will find out everything Django beginner should know.


Try this:

from django.conf.urls.defaults import patterns, include, url
from mysite.yourapp import views

urlpatterns = patterns('',
    url(r'^$', 'mysite.views.page1', name='home'),
    url(r'^page2/$', 'mysite.views.page2', name='page2'),
)

The r'^page1/$ bit is python regex

in your views.py file define your views:

def page1:
 #something

This should help you get started http://docs.djangoproject.com/en/1.3/intro/tutorial01/


Well, i found the most straight-forward way is:

1) urls.py

from django.urls import include, path
from . import views
from django.views.generic import TemplateView

urlpatterns = [
    path('test.html', TemplateView.as_view(template_name='main/test.html')),
]

2) templates/test.html

Hello world!

Classic 3-steps:

1) urls.py

from django.urls import include, path
from . import views

urlpatterns = [
    path('test.html', views.test, name='test'),]

2) views.py

from django.shortcuts import render

def test(request):
    return render(request, 'test.html')

3) templates/test.html

Hello world!


Thats more or less what I use:

urlpatterns = patterns('',
    (r'^$', 'news.views.page1'),
    (r'^page2/$', 'news.views.page2'),

)

just for your understanding: the beginning of a line is expressed as ^, the end as $. So ^$ stands for an empty line. more about regexp: http://docs.python.org/library/re.html


If you are using older django than direct_to_template generic view is what you need.

from django.views.generic.simple import direct_to_template

urlpatterns = patterns('',
    (r'^page1/$', direct_to_template, {'template': 'page1.html'}),
    (r'^page2/$', direct_to_template, {'template': 'page2.html'}),
)

Or for newer django 1.3 you need to use class based generic views

from django.conf.urls.defaults import *
from django.views.generic import TemplateView

urlpatterns = patterns('',
    (r'^page1/', TemplateView.as_view(template_name="page1.html")),
    (r'^page2/', TemplateView.as_view(template_name="page2.html")),
)

P.S. Don't forget to create page1.html and page2.html template files.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜