PyCharm - HTML file - Flask Inheritence using {% block content% } not working - python

I am trying to create template inheritance using Jinja2. I am using (basic.py) to define app and render "home.html". I created base.html as framework file that I want to inherit in home.html. Problem is that that the code
{% block content %}
{% endblock %}
in my base1.html file is not getting recognized when I run base.py. These lines of code are greyed out in PyCharm pro editor Also, the following code in home.html that I am using to extend base.html is also not working (greyed out):
{% extends [enter image description here][1]base1.html %}
{% block content%}
<h1>Hello, How are you?</h1>
{% endblock %}
I appreciate any help as there don't seem to be many direct answers available either on PyCharm site or anywhere else. Please see attached picture of code files. I do already have installed Flask package in my project.

i found a solution that worked. the solution is to select Jinja2 as the template language for for HTML template file type.enter image description here

Related

Customized Login site not showing in Django

I have created a Django App and want to provide a custom Login page with only the possibility to use a Google login.
I have implemented the Google login based on this post: https://www.section.io/engineering-education/django-google-oauth/
It actually works fine and when I hit localhost/account/login I am getting a login page:
I actually do not want a sign up and a "local" sign in option, I only need a Google login.
To achieve this I have created the following folder structure to overwrite the login template (based on https://learndjango.com/tutorials/django-login-and-logout-tutorial):
MyApp/templaltes/registration/login.html
login.html has the following content:
{% extends "base.html" %}
{% load i18n %}
{% load socialaccount %}
{% block content %}
<div class="row">
<center><h1>{% trans 'LoginHeading' %}</h1></center>
</div>
{% if user.is_authenticated %}
<p>Welcome, You are logged in as {{ user.username }}</p>
{% else %}
Login With Google
{% endif %}
{% endblock %}
I have also added 'DIRS': [str(BASE_DIR.joinpath('templates'))] to the TEMPLATES section in settings.py.
Unfortunately I am still seeing the Django default login page. What am I missing?
(I have also restarted the dev server to make sure it is not an issue there with the same result)
I have figured it out. After reading the documentation of Allauth throughly I found this:
For instance, the view corresponding to the account_login URL uses the
template account/login.html. If you create a file with this name in
your code layout, it can override the one shipped with allauth.
So I changed the folder structure to account/login.html and it works.
To do this thing you can simply create an account folder under the template and add login.html inside it.
This will override your login page.
Make sure that folder name must be account.
In your case, you can rename your folder from "registration" to "account".
Like following:
I am sure this will work well.
Simple solution is to add
SOCIALACCOUNT_LOGIN_ON_GET=True
to your settings.py and it should skip/bypass the sign up form.
Credits: https://stackoverflow.com/a/70680165/11605100
Still does not work?
Stop the server and then;
python manage.py migrate
python manage.py makemigrations
python manage.py runserver

Logo doesn't display using Django admin_interface

I have django-admin-interface installed in my django app. The problem is that when I try to upload a logo it says that all is okay but the logo doesn't show.
Logo uploaded correctly but not showing
I want to change the default Django logo.
Is there any way to accomplish this?
Create a new template and extends the 'admin' template like below to override the default admin template.
{% extends "admin/base.html" %}
{% block branding %}
<h1 id="site-name">MY_LOGO_OR_TEXT</h1>
{% endblock %}
In the code above, change the MY_LOGO_OR_TEXT section with your html logo or just text to be shown on the admin panel like you wanted.
ADMIN

Django, following a tutorial and there's an issue where my {% block content %} is not being recognized

Here's the dir structure:
directory structure
If the image isn't displaying here's a text version:
templates
myapp
new_search.html
base.html
base.html code:
{% block content %}
{% endblock content %}
new_search.html code:
{% extends "base.html" %}
{% block content %}
<h2>NEW SEARCH</h2>
{% endblock %}
I can display the base.html fine.
The new_search.html however displays like this:
new_search.html
I also want to mention a side question. My Django server isn't running however I can still open the html in my browser. Is that supposed to happen? My URLs when opening both html's are:
base.html:
http://localhost:63342/Full-Stack%20Web%20App/myapp/templates/base.html?_ijt=rtiq5iv3jude6ijjmu0ept2i82
new_search.html:
http://localhost:63342/Full-Stack%20Web%20App/myapp/templates/myapp/new_search.html?_ijt=rtiq5iv3jude6ijjmu0ept2i82
Also, i'm getting this error when trying to visit my home page:
error screenshot
EDIT:
Adding more info. I now get this error. And my new_search template still isn't including the base template. I tried moving the html to different directories etc. In the tutorial it matches too.
template Doesn't Exist
it says the source doesn't exist but I can see it right there. proof
You might want to try copying "base.html" in templates --> myapp folder.
Basically maintain both hmtl files in the same directory.
And also try giving full path after copying them in same dir, like:
{% extends "myapp/base.html" %}
For the home page, you need to have the tag {% load static %} to be able to use the static tag.
Make sure you are using the render() function in your view so it doesn't just return the template as text
https://docs.djangoproject.com/en/dev/topics/http/shortcuts/

HTML changes not reflected in django-cms

I was working on an old django-cms project and was trying to edit base.html file and no changes are reflected when I reload the page.
But if I delete all the lines in that file the django runserver refuses to start showing error
django.core.exceptions.ImproperlyConfigured: The 'js' and 'css' sekizai namespaces must be present in each template, - or a template it inherits from - defined in CMS_TEMPLATES. I can't find the namespaces in 'project/cms/home.html'.
They why isn't other changes like adding a new class not reflected in the page reload or server restart.
NOTE:
The project is working good as it is. I was trying to modify it a little bit. Changes I made in the css pages are getting reflected when I reload the page. Issue is only when I try to edit HTML pages
For base.html, you need to have {% load cms_tags sekizai_tags %} in the file. Add {% render_block "css" %} to <head></head> and {% render_block "js" %} somewhere between <body></body>. Depending on the template files that inherit from base.html, certain portions may have been overwritten. For example, if you had:
{# base.html #}
{% block content %}
<div class="example-class"></div>
{% endblock %}
But in another file say:
{# layout.html #}
{% extends "base.html" %}
{% block content %}
{% endblock %}
The div would not appear.
If however, you are talking about missing CSS files, you still need to include them in <head> for it to be displayed. render_block "css" is for django-cms css files that are included in plugins etc. I usually use a LESS or SCSS compiler to include CSS into my projects.
Hope that helps. Post more details for a better diagnosis.

'disqus_tags' is not a valid tag library error in Django

I'm getting this error no matter what I try:
TemplateSyntaxError
'disqus_tags' is not a valid tag library: Template library disqus_tags not found
In template /home/myproject/zinnia/templates/zinnia/entry_detail.html,error at line3
1 {% extends "base.html" %}
2 {% load i18n comments zinnia_tags thumbnail %}
3 {% load disqus_tags %}
4
The code I'm trying is working fine on server and another computer. What could be the problem?
Above you've stated that you have both disqus and django-disqus packages installed. I highly suspect they both provide a disqus python module. Remove the disqus==0.0.4 package and everything should work. django-disqus does not have any additional requirements.

Categories