Django - Nothing happens when "submit" button is clicked in a ModelForm? - django

Ok, so I am making a project, where you can make new blogposts and edit existing blogposts in Python with Django, but when I want to make a new post on my website, the "submit" button in new_post.html does nothing when I press it. I fill in the 'title' field and I fill in the 'text' field from the ModelForm "BlogPostForm" and press the button. In the terminal there's no POST or GET request. I just don't know why?
I'm using Python 3.6.4 and Django 2.1, installed in a virtual environment created by the "venv" module.
base.html:
<p>
Blogs
</p>
{% block content %}{% endblock content %}
index.html:
{% extends "blogs/base.html" %}
{% block content %}
<p>All posts:</p>
<ul>
{% for blog in blogs %}
<li>
{{ blog }}
<p>{{ blog.date_added|date:'M D, Y H:i' }}</p>
<p>{{ blog.text|linebreaks }}</p>
<p>-------------------------------------<p>
</li>
{% empty %}
<li>No posts are posted yet.</li>
<p>
Add new post
</p>
{% endfor %}
</ul>
<p>
Add new post
</p>
{% endblock content %}
new_post.html:
{% extends "blogs/base.html" %}
{% block content %}
<p>Make a new post:</p>
<post action="{% url 'blogs:new_post' %}" method='post'>
{% csrf_token %}
{{ post.as_p }}
<button name='submit'>Add Post</button>
</post>
{% endblock content %}
Thanks in advance!

There is no <post> tag in HTML. To post a data you need to use a <form> tag. Everything else is correct.

Related

Github Pages: adding an if condition in index.html

I have a blog created on Github Pages using Jekyll Now
The default Index.html looks like this
---
layout: default
---
<div class="posts">
{% for post in site.posts %}
<article class="post">
<h2>{{ post.title }}</h2>
<div class="entry">
{{ post.excerpt }}
</div>
Read More
</article>
{% endfor %}
</div>
This creates a landing page where the titles of all the posts you have made in the _posts directory are displayed with a link.
Looking at the {% for ... %} & {% endfor %} & the final static HTML, it seems as if when building the page, the for tag is actually iterated & the final HTML contains a list of actual titles.
I want to change this so that not all posts are listed. I do not want to list any post whose title contains the string "BEINGWRITTEN"
I tried stuff like
{% for post in site.posts %}
{% if (post.title.indexOf('BEINGWRITTEN') == -1) %}
<article class="post">
...
</article>
{% endif %}
{% endfor %}
Also tried with includes instead of indexOf, that also doesn't work. Both cases, I don't see any posts linked at all on the landing page.
How do I do this?
I did this by adding a category in the front matter of the page I don't want included.
i.e.
category: noshow
Then changed the index.html to
{% for post in site.posts %}
{% unless post.category == "noshow" %}
.....
{%endunless}
{%endfor}

How to use the teaser_text templatetag of cmsplugin-articles 0.2.2

I'm making a site for which I use cmsplugin-articles 0.2.2. That means that there's a page on the site (News) which has nested child pages which are the news. In the parent page you view a list of news teasers. (This is the way the plugin works.)
Looking into the template of the article teaser that the plugin provides:
{% load article_tags i18n %}
<div class="article">
{% block teaser_head %}
<time datetime="{{ article|published_at|date:"Y-m-d" }}">{{ article|published_at|date:"d.m.Y" }}</time>
<h2>{{ article|teaser_title }}</h2>
{% endblock %}
{% block teaser_body %}
{% with article|teaser_image as image %}
{% if image %}
<img src="{{ image.url }}" />
{% endif %}
{% endwith %}
<p>
{% teaser_text article %}
{% trans "More" %}
</p>
{% endblock %}
</div>
you can see the templatetag {% teaser_text article %} that should show some kind of text in the news teaser when rendering the News page.
I've published some news with some text for each one and when I go to the News page I can see the teasers list ok, but not any text, only the title, date, paginator and the "more" link, that proceed from other templatetags different than {% teaser_text %}.
Now the question is: does anybody knows how I can show each news text into each teaser (better an extract) when rendering the News page?

Conflict between 'editable' and Mezzanine form in Django/Mezzanine template

I'm getting a conflict between editable fields and a mezzanine form (i.e. a standard form created in the admin).
When I make the page title 'editable', the form at the bottom renders a field to edit the title instead of the actual form. As the editable fields only show when you are logged in, this is only an issue when I am logged in.
If I remove 'editable' on the title the form renders as it should when logged in and logged out.
The code that causes the problem:
<h1>{% editable page.form.title %}{{ page.form.title }}{% endeditable %}</h1>
{% if request.GET.sent %}
<div class="well">
{% editable page.form.response %}
{{ page.form.response|richtext_filter|safe }}
{% endeditable %}
</div>
{% else %}
{% with page.form as page_form %}
{% editable page_form.content %}
{{ page_form.content|richtext_filter|safe }}
{% endeditable %}
<form method="post">
{% fields_for form %}
<input type="submit" value="{{ page.form.button_text }}">
</form>
{% endwith %}
{% endif %}
Any ideas very much appreciated :)
As discussed on IRC - there's a name collision in the templates, with a fix in Mezzanine pending.

Unable to paginate object_list usng django-pagination

I am using django-pagination to paginate the a list of objects in my temlate. I have installed the app, added it in my project and added pagination.middleware.PaginationMiddleware in my settings.py file. But when I try to use it in my template the object_list is not being paginated. Here is my template code
{% extends "base.html" %}
{% load pagination_tags %}
{% autopaginate Questions %}
{% block title %}
Questions
{% endblock %}
{% block content %}
<div id="contentDiv">
{% for question in Questions %}
<div style="padding:5px 20px 5px 30px;">
<p class='question'><span id='style2'>Q
</span> {{ question.questiontext|safe }}
<span style= 'float:right;'><span style='font-size:12px; color:#099;'><a href="/question/type={{question.type}}"
style='font-size:12px; color:#099;'>{{question.type}}</a></span> <span style='color:#99C; font-size:12px;'>Level: </span><span style='color:#099;font-size:12px;'>{{question.level}}</a></span></span>
</p>
<h2 class='trigger1' ><a href='#'>Answer</a></h2>
<div class='toggle_container' >
<div class='block' style='background-color:#fff; '>
<p class='ans'> {{ question.answer|safe }} </p>
</div>
</div>
</div>
{% endfor %}
<div class="pagination" style="width:1000px; margin:auto; margin-bottom:20px;">
{% paginate %}
</div>
</div>
{% endblock %}
The list of objects is in the context_variable called Questions. Am I doing something wrong?
After a very long time I have been able to find out the error I was having with django-pagination. I had the canonical base template which I was extending on all pages.
In the documentation it is written that we require to put {% paginate %} after {% autopaginate object_list %} but no where it was written about the placement of {% autopaginate object_list %} itself.
I had title and body blocks in my template, and I was putting {% autopaginate object_list %} just below the {% extends "base.html" %} and as a result it was not working. I found that I had to put this statement inside the body block and now it is working absolutely fine.
Can you see the content of your pagination div if you write "Hello, I want a burger" or anything else in there?
Are you sure you have enough Questions to paginate? You could try something like:
{% autopaginate Questions 2 %}
to make sure that you'll be paginating at 2 questions/page.
Solved as Sachin told above:
I just moved {% load pagination_tags %}{% autopaginate list_objs 10 %}
inside {% block content %} statement (previously it was outside of it, so pagination was invisible. If no errors, but now pages - try to play with it (moving pagination block).

Do I need to create separate forms for simple and Ajax thing in Django

I have many forms which are working fine if i load them via normal http link.
The template is below
{% extends "app/base.html" %}
{% block title %}Create Account{% endblock %}
{% block media %} {% include "app/media_template.html" %} {% endblock %}
{% block heading %}Form{% endblock %}
{% block content %}
<div id="stylized" class="myform">
<form action="" method="post" enctype="multipart/form-data" >
<h1>Account form</h1>
<p>This is the basic look of my form without table</p>
{% csrf_token %}
{% for field in form %}
{{ field.errors }}
{{ field.label_tag }} {{ field }}
{% endfor %}
<button type="submit">Sign-up</button>
<div class="spacer"></div>
</form>
</div>
{% endblock %}
But if i have to display via ajax then i just need the div box containing form only , i don't nedd all other html
So i want that if JS is not working then those forms still work via hyperlink.
I have 6 forms , do i have to create seoarate templae if i call via ajax
If you need to use part of your template in several places, you can put that part in a separate template, and include it wherever you need:
{% include "foo/bar.html" %}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#include