#trying to link css to html template
1 messages · Page 1 of 1 (latest)
Some people don't feel included by the term "guys". Please respect that and try to use alternatives such as "folks", "friends" or "djangonauts" (source: https://heyguys.cc/)
Hi, do you have this in settings.py?
STATIC_URL = '/static/'
and do you have "django.contrib.staticfiles", in INSTALLED_APPS?
Show us what you have written in your template file
unfortunately no
<html lang="">
<head>
<meta charset="utf-8" />
<title>hi</title>
{% load static %}
<link href="{% static 'styleLogin' %}" rel="stylesheet" type="text/css" />
</head>
And what comes out in the browser?
Ok, show us the Django view that should be rendering this template.
I don't now if it is important but i load static on top of file
like 1 line of html file
def login(request):
return render(request, 'login.html')
I saw that some people put it within the head and worked fine
Ah, looking at the image you DM'd me, your code is different than what you wrote above. You've got:
{{% load static %}}
instead of
{% load static %}
Yead I edited it to {% load static %}
I was trying both
I am a beginner in Django its my first project and I got stuck here
should I import something in view.py ? or is unrelated
from django.contrib import admin
from django.urls import path
import MainPage.views
urlpatterns = [
path('admin/', admin.site.urls),
path('', MainPage.views.login)
]
So, it looks like you're viewing the template file directly instead of using the django view.
how to fix that
How are you running your django server?
And how are you getting to that page?
clicking on url that it gives me
The url it gives you probably doesn't say "login.html" in it anywhere, right?
it doesn't
So how did you get there?
I believe it is because I made it null in urlpatterns
path('', MainPage.views.login)
Close your browser and try clicking on that link again.
the text {% load static %} is gone
but its not linked with the css
the logo disappeared also
Okay, in that page, view source.
you don't have styleLogin.css
I added .css and still the same
then ?
What does the HTML for the <link> tag look like? Also, what about the <img> tag that you say no longer works? (Hint: The image file should also be in your static files)
<link href="/static/styleLogin.css" rel="stylesheet" type="text/css" />
Great, looks like that worked.
it didn't 😦
In what way?
oh wait
it did !
I closed the browser and opened it again and it did
regarding the logo
src= {static 'logo.png'} ?
src="{% static 'logo.png' %}" - same way you did the CSS. But remember that the logo.png file needs to be in your static folder.
perfect !
it worked
thank you so much guys
much appreciated
excuse my ignorance, I just started learning this framework
Ken can you now help me please?🥹
So, for reference, the problem you were having before that caused the {% load static %} to appear in the browser was probably because you double-clicked on login.html at some point, causing your browser to try to load the template directly. Since this doesn't go through Django, the django templating language doesn't get replaced with valid HTML.
I'll go take a look...
I didn't but I ran the html file from pycharm, will it cause the same problem ?
The template file doesn't know how to run itself, so the best guess that pycharm has is "open html file in the browser". So, don't do that.