简体   繁体   中英

Show already exist data in UpdateView

I have class which is the inheritance of UpdateView

class BotContentsUpdateView(LoginRequiredMixin, UpdateView):
    model = BotContents
    template_name = 'bot_contents/bot_contents_edit.html'
    
    form_class = UploadBotContentsForm
    success_url = reverse_lazy("bot-contents-list")
    success_message = "success!!"
    def get_initial(self):
    
        initial = super().get_initial()
        initial['name'] = BotContents.name
        return initial 

class UploadBotContentsForm(forms.ModelForm):
    name = forms.CharField(label="name")
    
    class Meta:
       model = BotContents
       fields = ["name"]

in bot_contents_edit.html

 {{ form.name }}

In this case it shows text box automatically.

It's quite easy but I want to show the original stored data in BotContents as placeholder.

I thought get_initial is for this purpose, but it doesn't work.

How can I do this?

You can render every field in custom way with passing in html: placeholder={{ form.name.value }} . You can firstfully check how is field generated automatically and use same classes and ids. It should look something like this:

<input 
type="text" 
name="name" 
value=""    # important to leave that empty
maxlength="100" 
class="textinput textInput form-control" 
id="id_name" 
placeholder="{{ form.name.value }}"   # here you pass your name as placeholder
required >

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