How to call django forms inlineformset into django templates - django

i am new to django and learning some from stackoverflow. Now i am creating a website for post with images and title. I found ways to connect my two models (images and post) at https://stackoverflow.com/a/62158885/13403211. it is working fine when i add post from admin. But i want to know how can i add those inlineformset fields into my template for user to fill in.Does anyone knows??
Here is the code i found. I copy the same code in my app to try.
models.py
class Item(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="items")
name = models.CharField(max_length=100)
class ItemImage(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
img = models.ImageField(default="store/default_noitem.jpg", upload_to=get_image_dir)
forms.py
from django import forms
from django.forms.models import inlineformset_factory
from .models import Item, ItemImage
class ItemImageForm(forms.ModelForm):
class Meta:
model = ItemImage
exclude = ()
class ItemForm(forms.ModelForm):
class Meta:
model = Item
fields = ["name",]
ItemImageFormSet = inlineformset_factory(
Item, ItemImage, form=ItemImageForm,
fields=['img'], extra=3, can_delete=True # <- place where you can enter the nr of img
)
views.py
class ItemCreateView(LoginRequiredMixin, SuccessMessageMixin, CreateView):
template_name = "items/add_item_form.html"
success_message = 'Item successfully added!'
form_class = ItemForm
def get_context_data(self, **kwargs):
data = super(ItemCreateView, self).get_context_data(**kwargs)
data['form_images'] = ItemImageFormSet()
if self.request.POST:
data['form_images'] = ItemImageFormSet(self.request.POST, self.request.FILES)
else:
data['form_images'] = ItemImageFormSet()
return data
def form_valid(self, form):
context = self.get_context_data()
form_img = context['form_images']
with transaction.atomic():
form.instance.user = self.request.user
self.object = form.save()
if form_img.is_valid():
form_img.instance = self.object
form_img.save()
return super(ItemCreateView, self).form_valid(form)
I have search on google and i did not find any related to this. Or am i doing wrong? Can someone help me?

Related

how to stop create view if already created django

please help me in making this application in django project
Here is models.py file code
from django.db import models
from bio.models import Profile
class Question(models.Model):
name = models.TextField(max_length=500)
def __str__(self):
return self.name
class Answer(models.Model):
question = models.ForeignKey(Question, related_name='question', on_delete=models.CASCADE)
profile = models.ForeignKey(Profile, related_name='profile', on_delete=models.CASCADE)
text = models.TextField(max_length=400)
created_on = models.DateTimeField(auto_now_add=True,null=True)
def __str__(self):
return "{0} on {1}".format(self.question, self.profile)
class Meta:
db_table = 'answer'
constraints = [
models.UniqueConstraint(fields=['profile', 'text', 'question'], name='unique answer')
]
here is views.py file code
class DetailViewMixin(object):
details_model = None
context_detail_object_name = None
def get_context_data(self, **kwargs):
context = super(DetailViewMixin, self).get_context_data(**kwargs)
context[self.context_detail_object_name] = self.get_detail_object()
return context
def get_detail_object(self):
return self.details_model._default_manager.get(pk=self.kwargs['pk'])
class AnswerCreate(DetailViewMixin, CreateView):
details_model = Question
context_detail_object_name = 'question'
model = Answer
form_class = NewAnswerForm
template_name = "qna/answer.html"
success_url = reverse_lazy('qna:list')
def form_valid(self, form):
form.instance.profile_id = self.kwargs.get('pk')
form.instance.question_id = self.kwargs.get('pk')
return super().form_valid(form)
here is my forms.py code
from django import forms
from .models import Answer
class NewAnswerForm(forms.ModelForm):
class Meta:
model = Answer
fields = ['text',]
def clean(self):
try:
Answer.objects.get(text=self.cleaned_data['text'].lower())
raise forms.ValidationError('Answer exists!')
except Answer.DoesNotExist:
pass
return self.cleaned_data
where am I going wrong????
I want that if user answers one question then he couldn't answer it again
how can i do form validation if object is already created
Add a UniqueConstraint to Answer so no combination of Question and Profile can be repeated:
https://docs.djangoproject.com/en/3.1/ref/models/constraints/#django.db.models.UniqueConstraint

Combine DetailView and UpdateView?

I am new to Django and I need to know how to have DetailView and UpdateView on the same Page.
I have two Models:
class Company(models.Model):
CustomerNo = models.AutoField(primary_key=True)
Company = models.CharField(max_length=200)
Str = models.CharField(max_length=200)
Zip = models.IntegerField()
City = models.CharField(max_length=200)
Name = models.CharField(max_length=200)
Phone = models.IntegerField()
Mobile = models.IntegerField()
Email = models.EmailField(max_length=200)
Web = models.CharField(max_length=200)
Info = models.CharField(max_length=200)
def __str__(self):
return self.Company
class Contact(models.Model):
Contact_Company = models.ForeignKey(Company, on_delete=models.CASCADE)
Contact_Name = models.CharField(max_length=200)
Contact_Phone = models.IntegerField()
Contact_Mobile = models.IntegerField()
Contact_Fax = models.IntegerField()
Contact_E_Mail = models.EmailField()
Contact_Web = models.CharField(max_length=200)
def __str__(self):
return self.Contact_Name
I want to build a page where I can see the company data from the first model and an update form for contacts realeted to the first model.
I enter the page with pk, from the previous page, its a DetailView for the first Model and with additionally context to list the Contact data with a for loop in Template.
I can use UpdateView to get data in the form and save it. but I don't know
how do display the realeted Company on the same page. Is there a way to use DetailView and UpdateView together?
I can use this UpdateView to change the Contact data, but I don't know how to include extra context from the first model to display the address on same page.
The success URL is wrong too.
I need to pass the pk from the first model so I can go back to the right list on previous page.
class ContactUpdate(UpdateView):
model = Contact
form_class = ContactCreateForm
template_name = 'customer/contact_update.html'
def get_success_url(self):
return reverse('customer_list', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(ContactUpdate, self).get_context_data(**kwargs)
return context
Maybe FormMixin is a solution, I used it to Display the Data from
first Model and form for second Model on same Page. But I am really stuck
to realize this with UpdateForm.
I hope you understand my problem, sorry for my english.
Thank you for your help.
Forms.py
from django.forms import ModelForm
from .models import Company
from .models import Contact
from django.forms import HiddenInput
from django import forms
class CompanyCreateForm(ModelForm):
class Meta:
model = Company
fields = '__all__'
class ContactCreateForm(ModelForm):
class Meta:
model = Contact
widgets = {'Contact_Company': forms.HiddenInput()}
fields = [
'Contact_Company',
'Contact_Name',
'Contact_Phone',
'Contact_Mobile',
'Contact_Fax',
'Contact_E_Mail',
'Contact_Web',
You need to add form in the detail view,
class PostDetailView(DetailView):
model = Post #your model name
template_name = 'detail.html' #your template
# here you will add your form
def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
context['contactupdateform'] = ContactCreateForm()
return context
# Add POST method
def post(self, request, slug):
post = get_object_or_404(Post, slug=slug)
form = contactupdateform(request.POST)
if form.is_valid():
# from here you need to change your post request according to your requirement, this is just a demo
obj = form.save(commit=False)
obj.post = post
obj.author = self.request.user #to add the user
obj.save()
return redirect('detail', post.slug) #add your url
Make sure you are adding the POST request correctly, according to your model and url. This is an outline you can refer.
To add the form in the HTML, you need to do this,
{% for form in contactupdateform %}
<-- Add Your CSRF token and form here --!>
{% endfor %}
You can import this (LoginRequiredMixin) and insert in the updateview as an argument as the contact is a user
then try putting this in the models.py file :
def get_absolute_url(self):
return reverse('customer_list', kwargs={'pk': self.pk})
and remove (get_success_url) from views.py
You might need these too in the updateview Class "ContactUpdate"
login_url = '/login/'
redirect_field_name = <-- template path(html) of the page you want to reverse to...
HOPE THIS HELPS...

How to make sure users only get to DetailView, Listview and UpdateView their own created objects

I've created a simple app where logged in users can can submit a session for a conference, view the results of their submission, view a list of their submissions, and edit their submissions (they should not have access to other users' submissions). I'm using django's class-based views (CreateView, DetailView, ListView, UpdateView).
I'm struggling with the permissions however. All the views, except updateview, work but if I type in the url directly using a non-logged in username I can see their submissions.
I also suspect that the permissions is the same reason I can't get the updateview to work.
What am I missing? And is there a better way to avoid using usernames and slugs in the Url? I can't seem to find any examples or tips in how to do this type of thing. I'm a beginner so probably miss some understanding of the fundamentals here and there.
I've tried to understand how the User model works because there I did manage to find a way to create, view and edit user details in a protected way. I relied on function views there though and can't seem to apply that approach to the submission app.
models.py
class Hsession(models.Model):
submitter = models.ForeignKey(User, related_name="submittersessions", on_delete=models.CASCADE)
submission_date = models.DateTimeField(auto_now=True)
session_title = models.CharField("session title", max_length=40, default='')
session_description = models.TextField("session description", max_length=350, default='')
slug = models.SlugField(allow_unicode=True, unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.session_title)
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse("submission:detail-single", kwargs={"username": self.submitter.username, "slug": self.slug})
urls:
urlpatterns = [
path("", views.CreateSubmission.as_view(), name="create"),
path("by/<username>/<slug>",views.SubmissionDetail.as_view(),name="detail-single"),
path("by/<slug>/edit",views.EditSubmission.as_view(), name="edit"),
path("by/<username>/",views.SubmissionList.as_view(), name="list"),
]
views.py
class CreateSubmission(LoginRequiredMixin, generic.CreateView):
fields = ('session_title', 'session_description', 'subject_category')
model = models.Hsession
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.submitter = self.request.user
self.object.save()
return super().form_valid(form)
class SubmissionList(LoginRequiredMixin, generic.ListView):
model = models.Hsession
template_name = "submission/user_hsession_list.html"
def get_queryset(self):
try:
self.hsession_submitter = User.objects.prefetch_related("submittersessions").get(
username__iexact=self.kwargs.get("username")
)
except User.DoesNotExist:
raise Http404
else:
return self.hsession_submitter.submittersessions.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["hsession_submitter"] = self.hsession_submitter
return context
class SubmissionDetail(LoginRequiredMixin, generic.DetailView):
model = models.Hsession
def get_queryset(self):
queryset = super().get_queryset()
return queryset.filter(
submitter__username__iexact=self.kwargs.get("username")
)
class EditSubmission(LoginRequiredMixin, generic.UpdateView):
model = models.Hsession
fields = ('session_title', 'session_description', 'subject_category')
template_name = 'submission/hsession_update.html'
success_url = 'submission/hsession_detail.html'
forms.py
class UserSubmissionForm(ModelForm):
class Meta:
model = Hsession
fields = ['session_title','session_description', 'subject_category']
class EditSubmissionForm(ModelForm):
class Meta:
model = Hsession
fields = ['session_title','session_description', 'subject_category']
You should use UserPassesTestMixin like this
from django.contrib.auth.mixins import UserPassesTestMixin
class EditSubmission(UserPassesTestMixin,LoginRequiredMixin, generic.UpdateView):
model = models.Hsession
fields = ('session_title', 'session_description', 'subject_category')
template_name = 'submission/hsession_update.html'
success_url = 'submission/hsession_detail.html'
def test_func(self):
//should return true if he have access
if self.request.user.is_authenticated:
slug = self.kwargs['slug']
obj = self.model.objects.get(slug=slug)
login_user = self.request.user
return login_user.pk == obj.submitter.pk
else:
return False
for more information UserPassesTestMixin
from django.contrib.auth.mixins import UserPassesTestMixin
class EditSubmission(LoginRequiredMixin, UserPassesTestMixin, generic.UpdateView):
model = models.Hsession
fields = ('session_title', 'session_description', 'subject_category')
template_name = 'submission/hsession_update.html'
success_url = 'submission/hsession_detail.html'
def test_func(self):
self.object = self.get_object()
if self.request.user == self.object.user:
return True
else:
return False
I'm using "UserPassesTestMixin" like this and it is working.

Setting form fields in django class based generic view CreateView

I'm using django's CreateView to add images to a book. I pass the book's id to the class based view as a parameter in the url. Form fields such as book and language are not rendered on the template, rather they're obtained with the help of the book's id.
# views.py
class PictureCreateView(CreateView):
model = Upload
fields = "__all__"
book_id = None
def get_initial(self):
initial = super(PictureCreateView, self).get_initial()
initial = initial.copy()
self.book_id = self.kwargs['book_id']
book = Book.objects.get(id=self.book_id)
initial['book'] = book
initial['language'] = language
initial['uploader'] = self.request.user
return initial
# set book_id so it used in the template
def get_context_data(self, **kwargs):
context = super(PictureCreateView, self).get_context_data(**kwargs)
context['book_id'] = self.book_id
return context
def form_valid(self, form, **kwargs):
print('Form is valid')
self.object = form.save()
files = [serialize(self.object)]
data = {'files': files}
response = JSONResponse(data, mimetype=response_mimetype(self.request))
response['Content-Disposition'] = 'inline; filename=files.json'
return super(PictureCreateView, self).form_valid(form)
def form_invalid(self, form):
print('Form invalid!')
print(form.errors)
data = json.dumps(form.errors)
return HttpResponse(content=data, status=400, content_type='application/json')
# models.py
class Upload(models.Model):
image = models.ImageField(upload_to=get_upload_path, help_text='Image to process')
uploader = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, related_name='uploader')
language = models.ForeignKey(Language, models.CASCADE)
book = models.ForeignKey(Book, models.CASCADE)
The problem is that I get an error saying the form is invalid, and the fields uploader, book and language are required. How do I resolve this?
The initial data is used to display the defaults when the form is initially displayed. It isn't used when those values are missing from the submitted form data. If fields like book and uploader are set from the URL or logged-in user, then you should leave them out of the form completely, instead of setting them in the initial data. You can then set the values on the instance in the form_valid method before the form is saved.
from django.contrib.auth.mixins import LoginRequiredMixin
class PictureCreateView(LoginRequiredMixin, CreateView):
model = Upload
fields = ['other_field1', 'other_field2', ...] # leave out book, language and uploader
def form_valid(self, form):
self.book_id = self.kwargs['book_id']
book = Book.objects.get(id=self.book_id)
form.instance.book = book
form.instance.language = ????
form.instance.uploader = self.request.user
return super(
The LoginRequiredMixin makes sure that only logged-in users can access the view.
You may want to use get_object_or_404 to handle the case where book_id refers to a book that does not exist.
One thought, initial doesn't fill the model for submission. You need to do that in init
def __init__(self):
super(PictureCreateView, self).__init__()
self.fields['book'] = self.initial['book']
self.fields['uploader'] = self.initial['uploader']
self.fields['language'] = self.initial['book']
Or, if you don't want to set the fields, make sure they are optional in your original model:
class Upload(models.Model):
uploader = models.ForeignKey('uploader', on_delete=models.CASCADE, null=True, blank=True)
book = models.ForeignKey('book', on_delete=models.CASCADE, null=True, blank=True)
language = models.ForeignKey('language', on_delete=models.CASCADE, null=True, blank=True)

Elegantly set a single attribute of a Model used in a OneToOneField using a ModelForm

I am able to update the height attribute of MyLittleModel using MyModelForm(OneToOneField(MyLittleModel)) as follows:
models.py
class MyLittleModel(models.Model):
height = models.IntegerField()
has_color = models.NullBooleanField(null=True, blank=True)
class MyModel(models.Model):
my_little_model = models.OneToOneField(MyLittleModel)
age = models.IntegerField()
is_male = models.BooleanField(default=False)
forms.py
class MyModelForm(forms.ModelForm):
height = forms.IntegerField(max_length=30)
class Meta:
model = MyModel
fields = ("height",
"age")
views.py
class MyUpdateView(UpdateView):
form_class = MyModelForm
model = MyModel
template_name = 'my_template.html'
def form_valid(self, form):
my_little_model = MyLittleModel.objects.create(form.cleaned_data["height"])
form.instance.my_little_model = my_little_model
form.instance.save()
return super(MyUpdateView, self).form_valid(form)
def get_success_url(self):
return reverse("my_list_view")
urls.py
urlpatterns = patterns('',
url(regex=r'^update/(?P<pk>\d+)/$', view=MyUpdateView.as_view(), name="my_update_view"),
)
I think this is not a good coding style because this forces you to modify your code in the ModelForm as well as in the View, it would be preferable that this would happen in only one location.
So is it possible to set a value to my_little_model.height without modifying my views code as I did?
Note: I don't like the title to this question, if someone has a suggestion for renaming it into something more readable please let me know.
I would do this by overriding your MyModelForm's save method, like so:
class MyModelForm(forms.ModelForm):
height = forms.CharField(max_length=30)
class Meta:
model = MyModel
fields = ("height",
"age")
def save(self, *args, **kwargs):
my_little_model = MyLittleModel.objects.create(height=self.cleaned_data["height"])
self.instance.my_little_model = my_little_model
self.instance.save()
return super(MyModelForm2, self).save(*args, **kwargs)
Then your view becomes:
class MyUpdateView(UpdateView):
template_name = "my_template.html"
form_class = MyModelForm
model = MyModel
def get_success_url(self):
return reverse("my_list_view")
This way, all your logic for updating the model information lives in the form - if you want to change what's being updated, you can do it all in one place.