0

I have a forms.py file as

class SocialMediaForm(forms.ModelForm):
    class Meta:
        model = SocialMedia
        fields = "__all__"
        exclude = ("doctor",)
        labels = {}
        widgets = {
            "whatsapp": forms.TextInput(attrs={"class": "form-control"}),
            "telegram": forms.TextInput(attrs={"class": "form-control"}),
            "facebook": forms.TextInput(attrs={"class": "form-control"}),
            "instagram": forms.TextInput(attrs={"class": "form-control"}),
            "linkedin": forms.TextInput(attrs={"class": "form-control"}),
        }

with a view.py file

class SocialMediaProfile(FormView):
    model = DoctorSocialMedia
    form_class = SocialMediaForm
    success_url = "."
    template_name = "doctor/social_media_profile.html"

the question is how could i pass a form instance to template in FormView view as SocialMediaForm(instace=someInstance) # in fun based views.

1 Answer 1

1

To update an instance of a model one should ideally be using UpdateView:

from django.views.generic.edit import UpdateView

class SocialMediaProfile(UpdateView):
    model = DoctorSocialMedia
    form_class = SocialMediaForm
    success_url = "."
    template_name = "doctor/social_media_profile.html"

This would automatically get the instance from the keyword arguments passed from the urls if they are named as pk or slug. You can set slug_url_kwarg or pk_url_kwarg if they are passed with a different name in the url to the view.

If you really need to pass some of your own keyword arguments to the form you should override get_form_kwargs (Note: This method would automatically pass the model instance (if exists) if the view inherits from ModelFormMixin). Example:

class SocialMediaProfile(FormView):
    model = DoctorSocialMedia
    form_class = SocialMediaForm
    success_url = "."
    template_name = "doctor/social_media_profile.html"

    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs.update({'some_extra_kwarg': 'my_data'})
        return kwargs
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for replay sir, there i faced two issues first if the related form doesn't exists it should return an empty form(i don't know) and second that what if user changed the pk in **url ** so he/she should not be able to change other models instance.. (don't know possible with which view)
@AliAref I don't understand what does "related form doesn't exists" mean? Also by "user changed the pk" do you mean the user manually editing the url?
i mean if there was no instances so it should be empty to create one. and yes changing pk manually from url. (sorry for bad explanations)
@AliAref please show your model DoctorSocialMedia in the question, by any chance is the field doctor a one to one field with the user.
i used updateview and passed my parameters to form now working :) Thaaanks so much sir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.