找不到静态文件图像

时间:2019-03-30 14:08:51

标签: django

我是Django的新手,正尝试做一些静态文件练习。 在项目Twenty3rdMarch下,我创建了一个静态文件夹,并在该文件夹下创建了图像。在图像文件夹中,我放置了一些图像。

这是我的设置。py:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES_DIR=os.path.join(BASE_DIR,'templates')
print(TEMPLATES_DIR)
STATIC_DIR=os.path.join(BASE_DIR,'static')

SECRET_KEY = 'mz^^exko@!2czk35u$w-qr&v8u(46(fp7#u41ctabvwf=mz9m&'


DEBUG = True

ALLOWED_HOSTS = []




INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'first_app',
]

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 = 'Twenty3rdMrach.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR,],
        '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'Twenty3rdMrach.wsgi.application'


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'udemy',
        'USER':'root',
        'PASSWORD':'Ravi@go123',
        'HOST':'localhost',
        'PORT':'3306',
    }
}


AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]



LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

STATIC_URL = '/static/'
STATICFILES_DIR=(STATIC_DIR,)

这是我的观点:

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader


def index(request):
    template=loader.get_template('first_app/index.html')
    my_dict={'insert_me':"Hello I have created my first dynamic Template"}
    return HttpResponse(template.render(my_dict,request))


def help(request):
    template=loader.get_template('first_app/help.html')
    context1={'help':'Helping Hand!!'}
    return HttpResponse(template.render(context1,request))

这是我的Twenty3rdMarch \ url.py:

from django.contrib import admin
from django.conf.urls import url
from django.conf.urls import include
from first_app import views

urlpatterns = [
    url(r'', views.index, name='index'),
    url(r'^first_app/',include('first_app.urls')),
    url(r'^help/',include('first_app.urls')),
    url(r'admin/', admin.site.urls)
]

这是我的first_app \ url.py:

 from django.conf.urls import url
    from first_app import views
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    from django.conf.urls.static import static

    urlpatterns= [
        url(r'', views.index, name='index'),
        url(r'help/',views.help,name='help'),]

    urlpatterns+=staticfiles_urlpatterns()

每当我访问URL http://127.0.0.1:8000/static/images/dj.jpg时,都会引发404错误。

0 个答案:

没有答案