简体   繁体   English

Django-使用没有静态文件的CSS显示HTML

[英]Django - display html with css without staticfiles

Currently learning basics of Django. 目前正在学习Django的基础知识。 I kind of understand the concept of url and views. 我有点理解url和视图的概念。 I've downloaded a bootstrap template and I wanted to set it as main page. 我已经下载了引导程序模板,并希望将其设置为主页。 I know, that I could redo all the page, making it template and put css files into static folder, then link it with url and it should probably work. 我知道,我可以重做所有页面,使其成为模板并将css文件放入静态文件夹,然后将其与url ,并且应该可以正常工作。 I managed to display the page creating a lambda HttpResponse function, but I am unable to link css there. 我设法显示了创建lambda HttpResponse函数的页面,但无法在此处链接CSS。 Is such thing possible? 这样的事情可能吗? Can I somehow drop a webpage with css into folder and link it, or do I have to do it django way? 我可以以某种方式将具有CSS的网页拖放到文件夹中并进行链接,还是必须以Django的方式进行?
I know, that django way is less messy and probably better, but this is for test and learning purposes only. 我知道,这种django方式不那么混乱,而且可能更好,但这只是出于测试和学习目的。
Sorry, if it was already asked, I tried looking for the answer before asking. 抱歉,如果已经问过,我尝试在问之前寻找答案。

复制您的CSS,并将其放在<style> </style>标记内的HTML页面中。

I'm guessing you're going that route because it seems complex to get the static file serving working. 我猜您正在沿着这条路走,因为要使静态文件服务正常工作似乎很复杂。 Serving your static files is something that's really easy to do - and once you've pointed your static file at something, you can just use if for testing anything: 提供静态文件是一件非常容易的事情-将静态文件指向某个文件后,就可以使用if测试任何东西:

https://docs.djangoproject.com/en/1.9/howto/static-files/ https://docs.djangoproject.com/zh-CN/1.9/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = ("/Users/you/path/to/static",)                                       

Then you just need to use static in the url for that page. 然后,您只需要在该页面的URL中使用static。

I use a base.html that is extended by all of my templates. 我使用所有模板扩展的base.html You can either have it {% include %} your css, or simply reference it. 您可以将其{% include %}用作CSS,也可以直接引用它。 So you can change 1 variable (I do it in my master context file ) and have all of your templates go along for the ride. 因此,您可以更改1个变量(我在我的主上下文文件中完成此操作 ),并使所有模板一起使用。 Eg 例如

In base.html: 在base.html中:

<html>
<head>
{% if testing %}
    <!-- directly include stylesheet in page -->
    <style>{% include "my.css" %}<style>
{% else %}
    <!-- standard stylesheet link -->
    <link rel='stylesheet' type='text/css' href='my.css'>
{% end %}
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html>

In your templates: 在您的模板中:

{% extends "base.html" %}
{% block content %}
     Hi, Mom!
{% endblock content %}

BTW, a base.html that has your full site design and makes very liberal use of {% block %} can make the majority of your view templates short and sweet. 顺便说一句, base.html具有完整的网站设计,并且非常自由地使用{% block %}可以使您的大多数视图模板简洁明了。 The template docs are your friend! 模板文档是您的朋友!

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

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