简体   繁体   中英

In Django, how to render static/base.html file on browser?

I've changed settings.py as follows.

import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'mytweets.urls'
STATICFILES_DIRS = [
    os.path.join(
        os.path.dirname(__file__),
        'static',
    ),
]
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        BASE_DIR + '/templates/'
    ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
        ],
        'loaders': (
             'django.template.loaders.filesystem.Loader',
             'django.template.loaders.app_directories.Loader',
        ),
    },
},]
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
...
STATIC_URL = '/static/'

I have changed views.py file as follows.

from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import View
class Index(View):
    def get(self, request):
        return HttpResponse('I am called from a get Request')
    def post(self, request):
        return HttpResponse('I am called from a post Request')

Then I have changed url.py file as follows.

from django.conf.urls import patterns, include, url
from django.contrib import admin
from tweets.views import Index
admin.autodiscover()
urlpatterns = [
    url(r'^$', Index.as_view()),
    url(r'^admin/', include(admin.site.urls)),
]

Finally I have made base.html file.

{% load staticfiles %}
<html>
<head>
    <title>Django</title>
    <link rel="stylesheet" href="{% static 
 'bootstrap/css/bootstrap.min.css' %}" media="screen">
</head>
<body>
    {% block content %}
    <h1 class="text-info">Hello Django!</h1>
    {% endblock %}
<script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
</body>
</html>

The tutorial says that When I execute $python manage.py runserver , I'll see "Hello Django" that depends on base.html . But I can see "I am called from a get Request" that depends on views.py . I wanna know what's wrong and How can I render base.html ? Thanks for your time.

Try this in views.py :

from django.views.generic import TemplateView

class Index(TemplateView):
    template_name = 'base.html'

In views.py you need to render your template as in Django Documetation and should do as the following code:

from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import View

class Index(View):
    template_name = 'base.html'

    def get(self, request):
          return render(request, self.template_name)

Modify your views.py to the following:

from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic import View
class Index(View):
    template_name = 'base.html'
    def get(self, request):
        return HttpResponse('I am called from a get Request')
    def post(self, request):
        return HttpResponse('I am called from a post Request')
    def get(self, request):
          return render(request, self.template_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM