My problem is not showing up form in the Django template. I'm using python 3.7.6 Django 3.2 Here is my code
....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
forms.py
from django import forms
from tasks.models import Task, TaskType
class TaskForm(forms.ModelForm):
name = forms.CharField(max_length=100,
required=True, widget=forms.TextInput(attrs={'class': 'form-control'}))
input_image = forms.ImageField(widget=forms.FileInput(
attrs={'class': 'form-control-file'}))
task_type = forms.ModelChoiceField(queryset=TaskType.objects.name.all(), widget=forms.Select(
attrs={'class': 'form-control'}))
class Meta:
model = Task
fields = ['name', 'input_image', 'task_type']
view.py
from django.shortcuts import render, redirect
from tasks.forms import TaskForm
def create_task(request):
if request.method == 'POST' and 'submit-task' in request.POST:
task_form = TaskForm(request.POST, request.FILES, instance=request.user)
if task_form.is_valid():
task_form.save()
return redirect(to='dashboard')
return render(request, 'users/dashboard.html', {'task_form': task_form})
dashboard.html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<div id="myModal">
<div >
<div >
<!-- Modal Header -->
<div >
<h4 >Upload your image</h4>
<button
type="button"
data-dismiss="modal"
></button>
</div>
<!-- Modal body -->
<div >
<div >
<label >Task name</label>
{{task_form.name}}
<div >
<select id="inputGroupSelect04">
<option selected>Choose your model</option>
{{task_form.task_type}}
</select>
<span >
<span >
Browse… {{task_form.image_input}}
</span>
</span>
<input type="text" readonly />
</div>
<img id="img-upload" />
</div>
</div>
<!-- Modal footer -->
<div >
<button
type="button"
data-dismiss="modal"
>
Close
</button>
<button type="button" name="submit-task">
Save changes
</button>
</div>
</div>
</div>
</div>
</form>
So, in the template, the form is not showing up. Please help me to fix it. Thank you so much
CodePudding user response:
def create_task(request):
if request.method == 'POST' and 'submit-task' in request.POST:
task_form = TaskForm(request.POST, request.FILES, instance=request.user)
...
return render(request, 'users/dashboard.html', {'task_form': task_form})
I pretty sure that you want to return dashboard.html
on GET
method,
however task_form
is creating only when POST
method. In other words task_form
does not exists.
You should define it before using:
def create_task(request):
if request.method == 'POST' and 'submit-task' in request.POST:
task_form = TaskForm(...)
...
else:
task_form = TaskForm(...) # task form defined for non POST methods
# or place it here
# task_form = TaskForm(...) # task form defined for non POST methods
return render(request, 'users/dashboard.html', {'task_form': task_form})
CodePudding user response:
the first thing in your code is you are passing an instance for a create method but the instance does not exist when you didn't create the record yet, the other problem is you are not supporting the get method. there is an example :
from django.shortcuts import render, redirect
from tasks.forms import TaskForm
from .models import Task
def create_task(request):
instance = Task.objects.filter(user=request.user)
if request.method == 'POST':
if instance:
task_form = TaskForm(request.POST, request.FILES, instance=request.user)
else:
task_form = TaskForm(request.POST, request.FILES)
if task_form.is_valid():
task_form.save()
return redirect(to='dashboard')
return render(request, 'users/dashboard.html', {'task_form': task_form})
else:
if instance:
task_form = TaskForm(instance=request.user)
else:
task_form = TaskForm()
return render(request, 'users/dashboard.html', {'task_form': task_form})