如何显示表单实例?

时间:2021-03-10 14:51:55

标签: python django

如何传递带有实际填充的配置文件信息的表单(如函数视图中的 ProfileUpdateForm(instance=x))。 我是这样做的,不知道如何将实际的配置文件实例传递给表单。类似于 UpdateView,但在我的 DetailView 类中

function getweather() {
    //
    // Note url below is time sensitive and only works into the future
    //
    url ="http://metwdb-openaccess.ichec.ie/metno-wdb2ts/locationforecast?lat=54.7210798611;long=-8.7237392806;from=2021-03-10T14:00:00Z;to=2021-03-10T14:00:00Z"

    var xml0 = UrlFetchApp.fetch(url, options).getContentText();

    var xml = XmlService.parse(xml0)
    var root = xml.getRootElement()
    var children = root.getChildren()
    var product = children[1].getChildren()
    //Logger.log (product)
    var time1 = product[0].getChildren()
    var location = time1[0].getChildren()
    var cloudiness = location[6]
    var cloudpc = cloudiness.getText()
    Logger.log (cloudpc)

}

1 个答案:

答案 0 :(得分:0)

如果您想更新模型的实例,您应该使用 UpdateView [Django docs]

from django.urls import reverse
from django.views.generic.edit import UpdateView

class ProfileDetailView(UpdateView):
    model = Profile
    context_object_name = 'profile'
    template_name = 'profiles/myprofile.html'
    form_class = ProfileUpdateForm
        
    def get_success_url(self):
        # Use `reverse` instead of `reverse_lazy`. You will get an error otherwise!
        return reverse('profiles:profile', kwargs={'pk': self.get_object().pk})

此外,如果需要将关键字参数传递给表单(此处为 instance,但我建议坚持使用 UpdateView),您应该覆盖 get_form_kwargs

class MyView(SomeGenericView):
    ...
    
    def get_form_kwargs(self):
        kwargs = super().get_form_kwargs()
        kwargs['some_kwarg'] = 'Some value'
        # Your use case implies below line
        # kwargs['instance'] = self.get_object()
        return kwargs