Django : Inheritance error- TemplateSyntaxError at / “Could not pass the remainder” - python

I have a website running using Django that uses the following dgango blocktag to render text within a base.html file.
{% block content %}
<h1>Welcome </h1>
<p1> This is the site template </p1>
{% endblock content %}
However, to describe a page content and not the site itself, I have the following page.html which is meant to inherit the code from base.html
{% extends “base.html” %}
{% block content %}
<h1>Greetings</h1>
<p>The_page_template</p>
{% endblock content %}
Changing views.py from render base.html to render page.html as follows causes the error.
from django.shortcuts import render
def index(request):
#return render(request, "base.html") // this works
return render(request, 'pages/page.html') // this does not
The uploaded image is a screenshot of the broken webpage. The best explanation I can offer is that there is an error in the spacing of the code or the pathing to page.html. With pathing I do notice the broken webpage does point to page.html. The following is a relevant statement from settings.py
‘DIRS’: [BASE_DIR / ‘site/templates’]
'APP_DIRS' : TRUE
For the sake of completeness, the following paths are described.
root/site/templates/base.html
root/pages/templates/pages/page.html
root/pages/views.py
root/site/settings.py
Any help appreciated in resolving this error - thanks

Related

Django Template changing html attribute based on slug and current request

When I tried to iterate through a list of post titles to create links in a django template the urls never match the requested path (matching the current web page should alter the formatting on the page for that list item). When I try and change things, Django spits out a 404 error which doesn't let me diagnose the problem or it "works" but not as expected.
the relevant url patterns (in blog app within project):
path('', views.index, name='blog_index'),
path('<slug:post>', views.blog_post, name='entries')
the relevant views functions (in blog app within project):
ef index(request):
try:
return render(request, 'blog/index.html', {'posts':blog_posts.keys})
except:
raise Http404()
def blog_post(request, post):
try:
return render(request, 'blog/post.html', {
'post_title':post,
'post_content':blog_posts[],
'posts':blog_posts.keys
})
except:
raise Http404()
the navigation template:
<ul>
{% for post in posts %}
{% url "blog:entries" "{{post}}" as blog_entry %}
<li><a href="/blog/{{post}}" {% if request.path == blog_entry %} class="active"{% endif %}>
<span class="item">{{post}}</span>
</a></li>
{% endfor %}
</ul>
I suspect the {% url "blog:entries" "{{post}}" as blog_entry %} in the template is not resolving correctly as when I replace the href with {% blog_entry %} it causes a 404 error.
I have tried hard coding it in, I have tried different ways of writing the url, I have tried changing the syntax (in case I am doing it incorrectly. I have checked the page source when I have run tests. I have never been able to get the class attribute to be "active" when on the matching page.
EDIT: The links all go to the correct pages, however the IF logic in the template does not work. i.e. None of the changes I have tried make result in reques.path == blog_entry resolving as True.
Also the above code snippets are part of an App within a project and I have included more of the views.py and urls.py file. Currently I have the "blog posts" stored in a dictionary at the top of views.py for testing.
Try this:
<ul>
{% for post in posts %}
{% url "blog:entries" post=post as blog_entry %}
<li><a href="/blog/{{post}}" {% if request.path == blog_entry %} class="active"{% endif %}>
<span class="item">{{post}}</span>
</a></li>
{% endfor %}
</ul>

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/

Django Template Not Extending

I'm trying to extend a base template in Django named 'base.html' in my main page 'index.html' and it does not work as expected. Rather than extending the template, the page just displays {% extends 'base.html' %}, plus the HTML in index.html when displaying the index page.
The 'base.html' page sits in the root of my templates folder and my 'index.html' page sits in templates/pages.
In 'base.html':
{% load static %}
...some code...
{% block content %}
{% endblock %}
...some code...
In 'index.html':
{% extends 'base.html' %}
{% load static %}
{% block content %}
...some code...
{% endblock %}
In views.py:
def index(request):
return render(request, 'pages/index.html')
In settings.py:
TEMPLATES = [{'DIRS': [os.path.join(BASE_DIR, 'templates)]
Expected Result:
Navbar
Content
Actual Result:
{% extends 'base.html' %}
{% load static %}
{% block content %}
Content
{% endblock %}
Like others mentioned, you are most likely viewing the html file directly.
You need to go to a view that makes use of the template which will render the final html.
I am not familiar with your project structure, so try doing this first:
in urls.py
from django.urls import path
from django.views.generic import TemplateView
urlpatterns = [
path('', TemplateView.as_view(template_name='index.html'))
]
and copy your index.html to the root of your templates folder.
then browse to http://localhost:63342, once you have it working, then you can work your way out to suit your project structure.
Not opening the base.html template. The view function points to the the child data file. When I remove the extends statement from that, the child block data displays. So it is that Django is not interpreting the extends statement in the data file correctly when it exists. I can find no discussion of what might cause that to happen. And no error messaging. Kind of makes a deal breaker out of django.
You are probably opening the template itself rather than calling the development server. If Django has a problem with any of those things, it throws an error. It does not spit out the template code.

Flask required form on home page

I am looking for some kind of example with flask using a required form on the home page of my site that requires the user to select an option before being allowed onto the rest of the site. The form is used for selecting a collection out of my mongo db database. I need to know what collection the user wants to use before going anywhere else on the site. Once this done I need to make sure I can use this information on my other route and views on my site.
What you want is to implement a login infrastructure.
using flask, you have a base template, where every other template is extending, what you can do is something like the following:
base.html:
{% if current_user.is_authenticated %}
<content>
{% block content %}
{% endblock %}
</content>
{% else %}
<login-form>
{% block content %}
{% endblock %}
</login-form>
{% endif %}
using this code, the content is shown only and only if the user is authenticated. in login-form HTML you should have a form to ask the credentials needed to authenticate the user and then allow them access to rest of the site.
in other template files, you continue to use the same practice:
dashboard.html:
{% extends 'base.html' %}
{% block content %}
<YOUR HTML CONTENT>
{% endblock %}
the content of dashboard.html is only shown to the user, if they are logged in(current_user.is_authenticated = True)
this is because dashboard.html is shown inside the content block of base.html witch is only shown or rendered if that condition is met.
you can use anything else instead of is_authenticated like the collection being selected or anything like that. the procedure to do it is the same.

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.

Categories