Django - CSS stops working when changing urls

So, I ran into a problem on my website where I created two separate html pages. Then I edited urls.py so that the URLs are different for the two pages, but css stops working if I do this. My code is below and I will explain it in more detail after.

part of my 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> 

How to include a title on every html page

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

Two URLs causing problems

 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"), 

Thus, the above shows how my URLs are configured at the moment, and I understand that this will only someday go into the contact information. When I change the URL as follows:

 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 stops working for both pages.

Initially, before I had 2 pages, the url looked like this:

 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) ) 

Why does CSS stop working?

Thanks.

+5
source share
1 answer

Static URLs are two directories; but your path now consists of three directories, so the url is incorrect.

You cannot use relative URLs for your static links. Use absolute ones instead:

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

even better, use the {% static %} tag, which takes the STATIC_URL value from your settings file.

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

All Articles