简体   繁体   中英

Invalid block tag on line 24: 'form.as_p', expected 'endblock'. Did you forget to register or load this tag?

I'm a new in Django and trying to do a app but now I'm having this error: "Invalid block tag on line 24: 'form.as_p', expected 'endblock'."

TEMPLATE

{% extends "base.html" %}

{% block title %}
    <title>Tarefas</title>
{% endblock title %}

{% block content %}
  <div class="content">
  {% if messages %}
    <div class="container">
      <br>
      {% for message in messages %}
      <div class="alert alert-info" role="alert">
        {{message}}
      </div>
      {% endfor %}
    </div>
  {% endif %}

      <div class="container tasks-box">
          <table class="table table-striped">
            <form method='POST'>
              {% csrf_token %}
              {% form.as_p %}
              <button type="submit" class="btn btn-secondary btn-sec">Adicionar</button>
            </form>
[...]

forms.py

from django.forms import ModelForm
from todolist.models import Tasks

class TaskForm(ModelForm):
    class Meta:
        model = Tasks
        fields = ['task','responsible']

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse
from todolist.models import Tasks
from todolist.forms import TaskForm
from django.contrib import messages
from django.contrib.auth.decorators import login_required

@login_required
def todolist(request):
    if request.method == 'POST':
        form = TaskForm(request.POST or None)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.manager = request.user
            instance.save()
        messages.success(request,("Tarefa adicionada"))
        return redirect('todolist')
    else:
        form = TaskForm
        all_tasks = Tasks.objects.filter(manager=request.user)
        all_users = Tasks.objects.all()
        return render(request,'todolist.html',{ 'form':form,
                                                'all_tasks':all_tasks,
                                                'all_users':all_users})

models.py

from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
# Create your models here.

User = get_user_model()
class Tasks(models.Model):
    manager = models.ForeignKey(User, on_delete=models.CASCADE,default=None, related_name='tasks_manager')
    task = models.CharField(max_length=300)
    done = models.BooleanField(default=False)
    responsible = models.ForeignKey(User, on_delete=models.CASCADE, default=None, related_name='tasks_responsible', blank=True, null=True)

    def __str__(self):
        return self.task

I tryed don't use {{ form }} tag in template and thats work. I think the problem is in views, but i can't figure out why. Someone can help me?

From form rendering options as documented

All you need to do to get your form into a template is to place the form instance into the template context. So if your form is called form in the context, {{ form }} will render its and elements appropriately.

There are other output options though for the / pairs:

{{ form.as_table }} will render them as table cells wrapped in <tr> tags
{{ form.as_p }} will render them wrapped in <p> tags
{{ form.as_ul }} will render them wrapped in <li> tags

So you have a typo error in {% form.as_p %} just replace this with {{ form.as_p }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM