在Django中加载静态页面以用于开发目的

时间:2016-04-05 18:36:59

标签: python django static django-urls

我是一名拥有0 Django经验的前端开发者。我正在开发一个现有的Django应用程序,需要在交给Python开发人员之前添加一些静态页面。我只需要添加这些静态页面作为参考,它们不会作为静态页面存在于实际站点上。做这个的最好方式是什么?这是我的url.py文件:

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.conf import settings
from views import issues
from django.shortcuts import redirect

urlpatterns = []


if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
)

urlpatterns += patterns('refeed.views.account',
    url(r'^account/$', 'index'),
    url(r'^account/login/', 'login', name='login'),
    url(r'^account/logout/', 'logout'),
    url(r'^account/create/', 'create'),
    url(r'^account/delete_user/(\d+)', 'delete_user'),
    url(r'^account/create_user/', 'create_user'),
    url(r'^account/edit_user/(\d+)', 'edit_user'),
    url(r'^account/unsubscribe/(?P<account_id>\d+)/(?P<key>[A-Fa-f0-9]+)$',
        'unsub_recipient', name='unsubscribe_from_account_lists'),
    url(r'^account/unsubscribe/(?P<account_id>\d+)/(?P<issue_id>\d+)/(?P<key>[A-Fa-f0-9]+)$',
        'unsub_recipient', name='unsubscribe_from_account_by_issue'),
    url(r'^account/(\d+)/newsletter_archive.xml$', 'view_archive_xml'),
    url(r'^account/(\d+)/rss.xml$', 'view_rss'),
)

urlpatterns += patterns('refeed.views.issues',
    url(r'^issues/$', 'index'),
    url(r'^issues/(\d+)$', 'view', name='view_issue'),
    url(r'^issues/create$', issues.create, name='create_issue'),
    url(r'^issues/create_ajax$', 'create_ajax', name='create_issue_ajax'),
    url(r'^issues/(\d+)/edit$', issues.edit, name='edit_issue'),
    url(r'^issues/(\d+)/delete$', 'delete'),
    url(r'^issues/(\d+)/edit_and_resend$', 'edit_and_resend'),
    url(r'^issues/(\d+)/sort_stories$','sort_stories'),
    url(r'^issues/(\d+)/newsletter$','newsletter', name='preview_newsletter'),
    url(r'^issues/(\d+)/create_campaign$','create_campaign'),
    url(r'^issues/(\d+)/send_test$','send_test'),
    url(r'^issues/(\d+)/send$','send'),
    url(r'^issues/(\d+)/send_confirmation$', 'send_confirmation', name='send_confirmation'),
    url(r'^issues/(\d+)/delete_story/(\d+)$','delete_story'),
    url(r'^issues/(\d+)/view_archived$','view_archived'),
    url(r'^issues/unsubscribe_requests_csv$', 'unsubscribe_requests_csv',
            name='unsubscribe_requests_csv'),
    url(r'^issues/(\d+)/stat_csv/(unsubscribed|stories|clicked|bounced|opened)/$', 'stat_csv', name='issue_stat_csv'),

)

urlpatterns += patterns('refeed.views.feeds',
    url(r'^feeds/$', 'list'),
    url(r'^feeds/create$', 'create', name='create_feed'),
    url(r'^feeds/(\d+)/edit$', 'edit'),
    url(r'^feeds/(\d+)/edit_ajax$', 'edit_ajax', name='edit_feed_ajax'),
    url(r'^feeds/create_ajax$', 'create_ajax', name='create_feed_ajax'),
    url(r'^feeds/(\d+)/delete$', 'delete'),
    url(r'^feeds/(\d+)/delete_ajax$', 'delete_ajax', name='delete_feed_ajax'),
)

urlpatterns += patterns('refeed.views.stories',
    url(r'^$', lambda x:redirect('/stories/import')),
    url(r'^stories/$', 'index'),
    url(r'^stories/spider_for_story_img$', 'spider_for_story_img', name='spider_for_story_img'),
    url(r'^stories/create$', 'create'),
    url(r'^stories/(\d+)/edit$', 'edit'),
    url(r'^stories/(\d+)/delete_image_ajax$', 'delete_image_ajax'),
    url(r'^stories/(\d+)/delete$', 'delete'),
    url(r'^stories/(\d+)/delete_ajax$', 'delete_ajax', name='delete_story_ajax'),
    url(r'^stories/(\d+)/unbookmark_ajax$', 'unbookmark_ajax', name='unbookmark_ajax'),
    url(r'^stories/move_to_issue$', 'move_to_issue'),
    url(r'^stories/import$', 'import_stories'),
    url(r'^stories/import_ajax$', 'import_stories_ajax', name='load_all_feeds'),
    url(r'^stories/update_order_ajax$', 'update_order_ajax', name='stories_update_order_ajax'),
    url(r'^stories/get_current_stories/(\d+)', 'get_current_stories', name='get_current_stories'),
    url(r'^stories/get_images_json$','get_images_json'),
    url(r'^stories/upload_image$','upload_image'),
    url(r'^stories/bookmarks$','index_bookmarks'),

)

urlpatterns += patterns('refeed.views.categories',
    url(r'^categories/$', 'list'),
    url(r'^categories/create$', 'create'),
    url(r'^categories/create_ajax$', 'create_ajax', name='create_category_ajax'),
    url(r'^categories/(\d+)/edit$', 'edit'),
    url(r'^categories/(\d+)/delete$', 'delete'),
    url(r'^categories/sort_categories','sort_categories'),
)

urlpatterns += patterns('refeed.views.magazine',
    url(r'^view/(\d*)$', 'view_toc'),
    url(r'^view/(\d*)/category/(\d*)$', 'view_cat'),
    url(r'^view/(\d*)/story/(\d*)$', 'view_story'),
    url(r'^view/(\d*)/resources$', 'resources'),
    url(r'^view/(\d*)/toc_json$', 'toc_json'),
    url(r'^view/style$', 'css_style'),
)

urlpatterns += patterns('refeed.views.setting',
    url(r'^settings/$', lambda x:redirect('/account')),
    url(r'^settings/magazine$', 'magazine'),
    url(r'^settings/newsletter$', 'newsletter', name='newsletter_settings'),
    url(r'^settings/packages$', 'list_packages'),
)

urlpatterns += patterns('refeed.views.recipient_lists',
    url(r'^recipient-lists/$', 'index', name='recipient_lists_index'),
    url(r'^recipient-lists/create$', 'create'),
    url(r'^recipient-lists/drag_csv_create$', 'drag_csv_create', name='drag_csv_create'),
    url(r'^recipient-lists/(\d+)/drag_csv_edit$', 'drag_csv_edit', name='drag_csv_edit'),
    url(r'^recipient-lists/(\d+)/edit$', 'edit'),
    url(r'^recipient-lists/(\d+)/delete', 'delete'),
    url(r'^recipient-lists/(?P<recipient_list_id>\d+)/csv/(?P<csv_type>valid|invalid|unsubs|dupes)/$', 'download_csv', name='recipient_list_csv'),
)

urlpatterns += patterns('refeed.views.images',
    url(r'^stories/(\d+)/images/(\d+)/recrop$', 'recrop'),
    url(r'^images/updateall$', 'updateall'),
)

urlpatterns += patterns('refeed.views.webhooks',
    url(r'^webhooks/mandrill$', 'mandrill_webhook', name='mandrill_webhook'),
)

2 个答案:

答案 0 :(得分:0)

您可以使用TemplateView直接呈现模板而无需编写视图。 你必须导入它,然后告诉它渲染一些东西:

from django.conf.urls import url
from django.views.generic import TemplateView

urlpatterns = [
    url(r'^about/', TemplateView.as_view(template_name="about.html")),
]

这是docs

答案 1 :(得分:0)

要在不必编写视图的情况下呈现django模板,可以使用TemplateView

在你的urls.py中:

from django.views.generic import TemplateView

# ... [other url confs] ...

urlpatterns += patterns('',
    url(r'^info/$', TemplateView.as_view(template_name="info.html")),
)

由于您的所有urlpatter都位于单个文件中并使用django的patterns,因此我们在上面的url模式中也使用了相同的方法。否则,您应该像documentation建议的那样声明URLConfs。