model form not rendering in django template - django

My models.py and this is my model for photos.
# home photo page
class Photos(models.Model):
photo_title = models.CharField(max_length=50, blank=False)
photo_description = models.CharField(max_length=50, blank=False)
photo_date = models.DateField(blank=False)
photo_location = models.CharField(max_length=50, blank=False)
photo_file = models.FileField(upload_to='photos', blank=False)
def __str__(self):
return self.photo_title
My forms.py this is the model form I made to render it as a form.
class UploadPhotosForm(forms.Form):
class Meta:
model = Photos
fields = '__all__'
my views.py these are my relavent imports and section I coded in view file.
from .forms import CampForm, ProjectForm, HikeForm, UploadPostsForm, UploadPhotosForm
posts = UploadPostsForm()
photo = UploadPhotosForm()
print(photo.as_p())
here this code should print the form as text into console isn't it?
But I don't have any console output. It seems like the nothing has been initialized to the photo variable isn't it?
I do not have any clue what happened.
context = {
'title': 'manage_wall',
'posts': posts,
'photo': photo,
}
return render(request, 'manager/manage_wall.html', context)
My template
{% block content %}
<div class="container">
<div class="row">
<div class="col">
<form action="" method="post">
{% csrf_token %}
{{photo.as_p}}
<input type="submit" value="Add">
</form>
</div>
<div class="col">
<form action="" method="post">
{% csrf_token %}
{{posts.as_p}}
<input type="submit" value=" Add">
</form>
</div>
</div>
</div>
{%endblock %}
As you can see here my photoForm is not rendering in the frontend can someone point out the mistake I have made not to render that form only while other forms are successfully rendering in the frontend. My Question is all other model forms rendered successfully why this is not displaying properly.

Your UploadPhotosForm is inherited from forms.Form(...)
class which does not contain model in Meta class so instead of inheriting from forms.Form class inherit from form.ModelForm(...)
here is final version of your code
class UploadPhotosForm(forms.ModelForm):
class Meta:
model = Photos
fields = '__all__'

I found the answer in models.py it should be forms.ModelForm
class UploadPhotosForm(forms.ModelForm):
class Meta:
model = Photos
fields = '__all__'
it is not rendering unless it is a ModelForm

Related

How to modify the display of a validation error in Django?

I wanted to create simple datepicker that does not accept back dates. Within my models.py I have defined MealDay class and standalone functionvalidate_pub_date.
The logic behin works just fine, but I do not understand the way Django is showing up the ValidationError("Date can't be past!").
Why this is where it is, and why it seems to be within <li> tag? Is there any possibilty to handle the error within the HTML template or any other way to add some html/css to it? There is how the error looks now:
models.py:
def validate_pub_date(value):
if value < timezone.now() - datetime.timedelta(days=1):
raise ValidationError("Date can't be past!")
return value
class MealDay(models.Model):
day = models.DateTimeField(default=timezone.now().day, validators = [validate_pub_date])
breakfast = models.TextField(max_length=100, blank=True)
lunch = models.TextField(max_length=100)
dinner = models.TextField(max_length=100, blank=True)
views.py
class MealdayCreateView(CreateView):
model = MealDay
template_name = "mealplanner/mealday_new.html"
form_class = CreateMealdayForm
forms.py
class CreateMealdayForm(ModelForm):
class Meta:
model = MealDay
fields = '__all__'
widgets = {
'day': forms.DateInput(attrs={'type':'date'}),
}
mealday_new.html
{% extends "mealplanner/base.html" %}
{% block content %}
<h1>Plan your meals!</h1>
<form action="" method="post"> {% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save">
</form>
{% endblock content %}
{% endblock content %}
in django model (validate_field_name) method is connected with .is_valid() method so when all fields of modelform not get correct input till it's raise a validation error.

How to estabalish ManyToMany relationship on instantiation of object through generic CreateView?

So I have two models. Deck and Card. When the user creates card it should be tied to a deck, in a ManyToMany relationship.
The card is created through the Generic Django create view, and I can't crack how I can assign the card to a deck, in this context. Any ideas on how I might solve this?
My CreateView
class CardCreateView(LoginRequiredMixin, CreateView):
model = Card
fields = ['question', 'answer']
def form_valid(self, form):
form.instance.creator = self.request.user
return super().form_valid(form)
def get_success_url(self):
return reverse('spaced_repitition-home')
def assign_card(self, deck_id): #It's here I don't get why it doesn't work
card = self.get_object()
deck = get_object_or_404(Deck, pk=deck_id)
card.decks.add(deck)
card.save()
Template that sends user to form (passes on deck_id)
{% for deck in decks reversed %}
<a href="{% url 'card-create' deck_id=deck.id %}">
<p> Add Card </>
{% endfor %}
Form Template
{% extends "spaced_repitition/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class=form-group>
<legend class="borders-bottom mb-4"> Create Card </legend>
{{ form|crispy }}
<div class=form-group>
<button class= "btn btn-outline-info" type="submit"> Create </button>
</div>
</fieldset>
</form>
{% endblock content %}
Models
class Deck(models.Model):
title = models.CharField(max_length=100)
date = models.DateTimeField(default=timezone.now)
creator = models.ForeignKey(User, on_delete=models.CASCADE)
description = models.TextField(max_length=200, blank=True)
def __str__(self):
return self.title
class Card(models.Model):
question = models.CharField(max_length=100)
answer = models.TextField()
date = models.DateTimeField(default=timezone.now)
creator = models.ForeignKey(User, on_delete=models.CASCADE)
decks = models.ManyToManyField(Deck)
days_till_study = models.IntegerField(default=1)
def __str__(self):
return self.question + ' pk: ' + str(self.pk)
Url
path('card/new/<int:deck_id>/', CardCreateView.as_view(), name='card-create'),
Thanks for reading this.
Edit
Found the solution here:
Django Createview default value for a foreign key field

Django forms how to display related data in an inner form

I am struggling with Django forms.
I have the following model.py:
class Property(models.Model):
portfolio = models.ForeignKey("portfolios.Portfolio", on_delete=models.CASCADE)
class PropertyImage(models.Model):
property = models.ForeignKey("Property", on_delete=models.CASCADE)
image = models.ImageField(upload_to = property_image_upload_to)
def __str__(self):
return self.image.url
class PropertyDocument(models.Model):
property = models.ForeignKey("Property", on_delete=models.CASCADE)
document = models.FileField()
class Address(models.Model):
property = models.OneToOneField("Property", on_delete=models.CASCADE)
line1 = models.CharField(max_length=100)
line2 = models.CharField(max_length=100, null=True, blank=True)
line3 = models.CharField(max_length=100, null=True, blank=True)
post_code = models.CharField(max_length=7)
town = models.CharField(max_length=100, null=True, blank=True)
city = models.CharField(max_length=100)
When adding/updating a property, I want the form to show the form for related objects like the address, documents/images instead of the select list's that appear in forms - I want to be able to add/edit the related data.
My view.py file
class PropertyCreate(CreateView):
model = Property
form_class=PropertyAddressFormSet
success_url = reverse_lazy('Property_list')
def get_context_data(self, **kwargs):
data = super(PropertyCreate, self).get_context_data(**kwargs)
return data
Property_form.html
{% extends 'base/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<form method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" class="btn btn-primary" />
<button class="btn btn-link" onclick="javascript:history.back();">Cancel</button>
</form>
{% endblock %}
urls.py
from . import views
app_name = 'properties'
urlpatterns = [
path('<int:portfolio_id>/<int:pk>/edit', views.PropertyUpdate.as_view(), name='property_edit'),
path('<int:portfolio_id>/create', views.PropertyCreate.as_view(), name='property_new'),
]
I've read about inlineformset_factories and inlineformset's etc, but is this the best choice for my scenario? If so, I can't figure out how to show the portfolio, address form
I;m currently using a inlineformset like so, which creates the Address form on the PropertyCreate view, but I want to also add in the PropertyImages and PropertyDocs to the ProertyCreate view.:
PropertyAddressFormSet = inlineformset_factory(
parent_model=Property,
model=Address,
form=AddressForm,
extra=0,
min_num=1
)
For anyone in the same boat as me, I managed to get this working with the following code:
Forms.py:
class PropertyForm(ModelForm):
""" Edit a property """
class Meta:
model = Property
exclude = ()
PropertyAddressFormSet = inlineformset_factory(
parent_model=Property,
model=Address,
form=AddressForm,
extra=0,
min_num=1
)
Views.py
class PropertyCreate(CreateView):
model = Property
form_class=PropertyForm
success_url = reverse_lazy('Property_list')
def get_context_data(self, **kwargs):
data = super(PropertyCreate, self).get_context_data(**kwargs)
if self.request.POST:
data['address'] = PropertyAddressFormSet (self.request.POST, instance=self.object)
else:
data['address'] = PropertyAddressFormSet ()
return data
template:
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form |crispy }}
<fieldset class="border p-2">
<legend class="w-auto">Address</legend>
{{ address.management_form }}
{% for form in address.forms %}
<div >
{{ form.as_p }}
</div>
{% endfor %}
</fieldset>
</form>
Hope this helps someone.

Django - Display imagefield in ManytoMany form instead of title

I am working on a Django project with crispy forms.
I want to use images instead of the the default Models title/label to select a instance in a Many to Many relation form.
Content models.py:
class Cloth(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL)
title = models.CharField(max_length=200)
picture = ImageCropField(upload_to='cloth_pics/%Y-%m-%d/',
blank=True)
def __str__(self):
return self.title
class Outfit(models.Model):
owner = models.ForeignKey('profiles.Profile')
title = models.CharField(max_length=200)
cloths=models.ManyToManyField(Cloth)
Content forms.py
class ClothForm(forms.ModelForm):
class Meta:
model = Cloth
fields = ('title','type','picture')
class OutfitForm(forms.ModelForm):
class Meta:
model = Outfit
exclude= ["owner"]
Content views.py
def outfits_new(request):
if request.method == "POST":
form = OutfitForm(request.POST)
if form.is_valid():
outfit = form.save(commit=False)
outfit.owner = get_user(request)
outfit.created_date = timezone.now()
outfit.save()
pk=outfit.id
return HttpResponseRedirect(reverse('outfit_edit_delete', args=[pk]))
else:
cloths = Cloth.objects.filter(owner=request.user.id)
form = OutfitForm()
return render(request, '../templates/outfits_new.html', {'form': form, "cloths":cloths})
Content outfits_new.html
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
{{ form|crispy }}
<div class="btn-group" role="group" aria-label="Basic example">
<input type="submit" value="Submit" name="edit" class="btn btn-success">
</div>
This code produces a Outfit form where I can select different cloths( displaying the cloths title). I want to select different cloths using a image from the cloths.picture field.
Thank you very much,
Patrick
Have a look at select2 at https://select2.github.io/examples.html. It allows you to do images in comboboxes
There is a Django package at https://github.com/applegrew/django-select2

How to show ManyToMany fields using CreateView in Django 1.8

I'm doing a basic application in Django 1.8 and I'm using Create-View, I don't know why the create form doesn't have manyTOmany fields neither foreign-key field previously defined in my model. This is my code:
My Model:
class Autor(models.Model):
nombre = models.CharField(max_length=30)
....
class Editor(models.Model):
nombre = models.CharField(max_length=30)
...
class Libro(models.Model):
titulo = models.CharField(max_length=100)
autores = models.ManyToManyField(Autor) #MANY2MANY FIELD
editor = models.ForeignKey(Editor) #FOREIGN KEY FIELD
fecha_publicacion = models.DateField()
portada = models.ImageField(upload_to = 'portadas/')
def __unicode__(self):
return self.titulo
My View:
class LibroCreateView(CreateView):
model = Libro
template_name = 'biblioteca/crear.html'
fields = ['titulo', 'autores', 'editor', 'fecha_publicacion', 'portada']
My template:
{% block main %}
<form action="" enctype="multipart/form-data" method="POST">{% csrf_token %}
<table>
{{form.as_table}}
</table>
<input type="submit" name="crear" value="Crear">
</form> <br>
{% endblock main %}
My result
Why isn't my fields "Autores"(many2many) and "Editor"(foreign-key) correctly shown?. Thanks.
Try giving form to the view that is CreateView. Make a ModelForm using your model
There you can do query for you foreign key and many to many field.
You can present them as you like