简体   繁体   English

如何使用Django在网页上添加CSS /图像?

[英]How can I add css/image on webpage using django?

I put the image("hi.png") and css file in /home/user1/djangoblog/blog/static 我把图像(“ hi.png”)和css文件放在/ home / user1 / djangoblog / blog / static

In templates i have <img src="{{ STATIC_URL }}images/hi.png" /> 在模板中,我有<img src="{{ STATIC_URL }}images/hi.png" />

I edited the settings.py => 我编辑了settings.py =>

STATIC_ROOT = ''
STATIC_URL = "/static/"

STATICFILES_DIRS = (
    "/home/user1/djangoblog/blog/static/",)

The web page source returns => 网页源返回=>

<div><img src="/static/images/a.jpg" /></div>

If the views.py => The image is not displayed on the web page. 如果views.py =>图片未显示在网页上。

now = datetime.datetime.now()
#return render_to_response('current_datetime.html', {"foo": now}, context_instance=RequestContext(request))
t = loader.get_template('current_datetime.html')
c = Context({'foo': now})
return HttpResponse(t.render(c))

But if the views.py => it displays the images on the web page. 但是,如果views.py =>,它将在网页上显示图像。

return render_to_response('current_datetime.html', {"foo": now}, context_instance=RequestContext(request))

It looks like it's a problem with HttpResponse or RequestContext. 看来这是HttpResponse或RequestContext的问题。 How can i fix this problem with HttpResponse or RequestContext? 如何使用HttpResponse或RequestContext解决此问题?

You need to read the staticfiles docs more carefully. 您需要更仔细地阅读staticfiles文档。

You specify the absolute path to your static directory with STATIC_ROOT and the URL that directory can be accessed at through a browser via STATIC_URL . 您可以使用STATIC_ROOT指定静态目录的绝对路径,以及可以通过浏览器通过STATIC_URL访问该目录的URL。 Your main static directory should never go in STATICFILES_DIRS . 您的主静态目录应该永远不会进入STATICFILES_DIRS That setting is only to allow you to specify additional directories that should be processed while running the collectstatic management command. 该设置仅允许您指定在运行collectstatic管理命令时应处理的其他目录。

In general, you never use your actual static directory. 通常,您永远不会使用实际的静态目录。 In development Django serves files from each app's static directory and any directories in STATICFILES_DIRS (excluding the main static directory). 在开发中,Django从每个应用程序的static目录和STATICFILES_DIRS任何目录(不包括主static目录)提供文件。 In production, you run the collectstatic management command and then setup your main webserver (Apache, Nginx, etc) to serve files from STATIC_ROOT . 在生产环境中,您可以运行collectstatic管理命令,然后设置主Web服务器(Apache,Nginx等)来提供STATIC_ROOT文件。 Django never serves anything from the main static directory ( STATIC_ROOT ). Django 永远不会从主static目录( STATIC_ROOT )提供任何服务。

I suspect the problem is that you don't have the staticfiles context processor in your settings file. 我怀疑问题是您的设置文件中没有staticfiles上下文处理器。 That's the reason that /static/ isn't being inserted where you've put {{ STATIC_URL }} . 这就是未在您放置{{ STATIC_URL }}插入/static/的原因。

It's as simple as putting 就像放一样简单

'django.core.context_processors.static',

Into the list of context processors ( TEMPLATE_CONTEXT_PROCESSORS ) in your settings.py file. 进入settings.py文件中的上下文处理器列表( TEMPLATE_CONTEXT_PROCESSORS )。

Update 更新

I added this in TEMPLATE_CONTEXT_PROCESSORS but still that didn't work. 我在TEMPLATE_CONTEXT_PROCESSORS中添加了此功能,但仍然无法正常运行。 – guru 3 hours ago –大师3小时前

Adding 'django.core.context_processors.static' to the list of context processors did fix your problem because as you can now see, it is inserting the value for STATIC_URL wherever it sees {{ STATIC_URL }} in your templates. 'django.core.context_processors.static'添加到上下文处理器列表确实解决了您的问题,因为如您现在所见,它将在模板中看到{{ STATIC_URL }}位置插入STATIC_URL的值。 The error is that you've changed the value of STATIC_URL to "/home/user1/djangoblog/blog/static/" probably because you've been confused by some of the answers here. 错误是您已将STATIC_URL的值STATIC_URL"/home/user1/djangoblog/blog/static/"可能是因为您对此处的某些答案感到困惑。

Change the value of STATIC_URL back to '/static/' so that line should read STATIC_URL的值STATIC_URL '/static/'以便该行应显示为

STATIC_URL = '/static/'

That will fix your problem. 那将解决您的问题。

Django templates simply put whatever is in the variable into the template . Django模板只是将变量中的内容放入模板中 They don't care what it is or what it represents. 他们不在乎它是什么或代表什么。 If the value of STATIC_URL is /home/user1/djangoblog/blog/static/ then it dutifully inserts /home/user1/djangoblog/blog/static/ wherever it sees {{ STATIC_URL }} which is why you are seeing 如果STATIC_URL的值为/home/user1/djangoblog/blog/static/则它会在看到{{ STATIC_URL }}任何位置忠实地插入/home/user1/djangoblog/blog/static/ ,这就是您看到的原因

<img src="/home/user1/djangoblog/blog/static/images/a.jpg" />

whenever it renders the template containing <img src="{{ STATIC_URL }}images/a.png" /> 每当它呈现包含<img src="{{ STATIC_URL }}images/a.png" />的模板时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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