开发者

Django template not loading javascript and css properly due to urlpatterns

开发者 https://www.devze.com 2022-12-25 07:56 出处:网络
When this one runs everything goes fine: (r\"^newobject$\", \"views.myobjects.newobject\"), All the CSS + JS files are properly fetched from:

When this one runs everything goes fine:

(r"^newobject$", "views.myobjects.newobject"),

All the CSS + JS files are properly fetched from:

static/css/...
static/js/...

When this one runs:

(r"^mybjects开发者_StackOverflow/(([a-z]|[A-Z]|[0-9])+)$","views.myobjects.loadobject"),

All the css and JS files that are being fetched, are run trough the urlpatterns and are returning my defailt page:

(r"", 'views.main.index'),

This makes all my CSS and JS code to actualy be HTML. My guess is that i'm giving some noob mistake. Is there any common reason why this should happen? And how to fix it?

Edit:

Css example:

<link href="static/css/style.css" type="text/css" rel="stylesheet">

JS example:

<script src="static/js/libs/date.js" type="text/javascript"></script>


See the difference:

  • when you access *some url*/newobject the static/css/style.css refers *some url*/static/css/style.css*
  • when you access *some url*/newobject/whatever the static/css/style.css refers *some url*/newobject/static/css/style.css*

If your URL will always be floating around in depth, include your javascript and CSS using URLs relative to the server root (start them by /) instead of relative to the current dir.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号