【问题标题】:Django - CSS stops working when I change urlsDjango - 当我更改网址时,CSS 停止工作
【发布时间】:2015-11-16 14:14:29
【问题描述】:

所以我在我的网站上遇到了一个问题,然后我创建了两个单独的 html 页面。然后我编辑了 urls.py,因此 2 个页面的 url 会有所不同,但如果我这样做,css 将停止工作。我的代码如下,后面会详细解释。

我的 head.html 的一部分

<!-- Bootstrap core CSS -->


<link href="../../static/textchange/index.css" rel="stylesheet">

<!-- Custom styles for this template -->
<link href="../../static/textchange/jumbotron.css" rel="stylesheet">

<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
<!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
<script src="../../static/textchange/index.js"></script>

我如何在每个 html 页面上包含 head

{% include "textchange/head.html" %}

导致问题的两个网址

url(r'^results/(?P<uisbn>(\w)+)/(?P<uuser>(\w)+)$', views.contactpost, name="contactpost"),
url(r'^results/(?P<uisbn>(\w)+)/(?P<uuser>(\w)+)$', views.contactwish, name="contactwish"),

所以以上是我的网址目前的设置方式,我意识到目前这只会发送到联系人邮件。当我像这样更改网址时:

url(r'^results/(?P<uisbn>(\w)+)/post/(?P<uuser>(\w)+)$', views.contactpost, name="contactpost"),
url(r'^results/(?P<uisbn>(\w)+)/wish/(?P<uuser>(\w)+)$', views.contactwish, name="contactwish"),

CSS 对两个页面都停止工作。

最初在我有 2 个页面之前,网址看起来像这样:

url(r'^results/(?P<uisbn>(\w)+)/(?P<uuser>(\w)+)$', views.contact, name="contact"),

Views.py

@login_required
def contactpost(request, uuser, uisbn):
    ltextbook = Textbook.objects.filter(isbn = uisbn)
    text = ltextbook[0]
    luser = User.objects.filter(username = uuser)
    quser = luser[0]
    post = Posting.objects.filter((Q(user = quser) & Q(textbook = ltextbook)))
    posting = post[0]
    return render_to_response(
        'textchange/contactpost.html',
        locals(),
        context_instance=RequestContext(request)
        )

@login_required
def contactwish(request, uuser, uisbn):
    ltextbook = Textbook.objects.filter(isbn = uisbn)
    text = ltextbook[0]
    luser = User.objects.filter(username = uuser)
    quser = luser[0]
    wish = Wishlist.objects.filter((Q(user = quser) & Q(textbook = ltextbook)))
    wishlist = wish[0]
    return render_to_response(
        'textchange/contactwish.html',
        locals(),
        context_instance=RequestContext(request)
        )

为什么 CSS 会停止工作?

谢谢。

【问题讨论】:

标签: python css django url


【解决方案1】:

静态的 URL 上升了两个目录;但是你的路径现在是三个目录深,所以 URL 是错误的。

您不应该对静态链接使用相对 URL。相反,使用绝对的:

<link href="/static/textchange/index.css" rel="stylesheet">

更好的是,使用{% static %} 标记,它从您的设置文件中获取 STATIC_URL 的值。

<link href="{% static "textchange/index.css" %}" rel="stylesheet">

【讨论】:

  • 对不起,回复晚了,效果很好,我会记住这一点!谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多