質問編集履歴
2
修正
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -180,10 +180,10 @@ | |
| 180 180 | 
             
                def form_valid(self, form):
         | 
| 181 181 | 
             
                    form.save()  # ここで保存
         | 
| 182 182 |  | 
| 183 | 
            -
                    for  | 
| 183 | 
            +
                    for f in form:
         | 
| 184 | 
            -
                        Purchase_history.objects.create(name=cleaned_data["name"],
         | 
| 184 | 
            +
                        Purchase_history.objects.create(name=f.cleaned_data["name"],
         | 
| 185 | 
            -
                        price=cleaned_data["price"])
         | 
| 185 | 
            +
                        price=f.cleaned_data["price"])
         | 
| 186 | 
            -
             | 
| 186 | 
            +
                        
         | 
| 187 187 | 
             
                    return super().form_valid(form)
         | 
| 188 188 |  | 
| 189 189 |  | 
| @@ -217,4 +217,19 @@ | |
| 217 217 | 
             
                class Meta:
         | 
| 218 218 | 
             
                    model = Purchase_history
         | 
| 219 219 | 
             
                    fields = ("name", "price")
         | 
| 220 | 
            -
            ```
         | 
| 220 | 
            +
            ```
         | 
| 221 | 
            +
             | 
| 222 | 
            +
            ``` Local vars
         | 
| 223 | 
            +
            __class__	
         | 
| 224 | 
            +
            <class 'stockpile.views.Shopping_listCreateView'>
         | 
| 225 | 
            +
             | 
| 226 | 
            +
            f	
         | 
| 227 | 
            +
            <Shopping_listForm bound=True, valid=True, fields=(name;price;id;DELETE)>
         | 
| 228 | 
            +
             | 
| 229 | 
            +
            form	
         | 
| 230 | 
            +
            <django.forms.formsets.Shopping_listFormFormSet object at 0x7f1e95ec36a0>
         | 
| 231 | 
            +
             | 
| 232 | 
            +
            self	
         | 
| 233 | 
            +
            <stockpile.views.Shopping_listCreateView object at 0x7f1e95df7518>
         | 
| 234 | 
            +
            ```
         | 
| 235 | 
            +
            fオブジェクトにはnameが存在しているように見えるのですが、エラーとなります
         | 
1
修正したコードを追記
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -136,4 +136,85 @@ | |
| 136 136 | 
             
            ・アップデートビューから変遷先のテンプレートにリダイレクト
         | 
| 137 137 |  | 
| 138 138 |  | 
| 139 | 
            -
            で、実装できるのかな。と考えておりましたが、そもそもそのやりかたも分からず、ご教授頂きたいです。
         | 
| 139 | 
            +
            で、実装できるのかな。と考えておりましたが、そもそもそのやりかたも分からず、ご教授頂きたいです。
         | 
| 140 | 
            +
             | 
| 141 | 
            +
            追記:修正しましたが解決に至っておりません。コードは下記の通りです。
         | 
| 142 | 
            +
            ```python
         | 
| 143 | 
            +
            # views.py
         | 
| 144 | 
            +
             | 
| 145 | 
            +
            # Shopping_listView
         | 
| 146 | 
            +
             | 
| 147 | 
            +
            class Shopping_listView(ListView):
         | 
| 148 | 
            +
                model = Shopping_list
         | 
| 149 | 
            +
                paginate_by = 30
         | 
| 150 | 
            +
                template_name = "stockpile/shopping_list.html"
         | 
| 151 | 
            +
                queryset = Shopping_list.objects.all()
         | 
| 152 | 
            +
                
         | 
| 153 | 
            +
                def get_context_data(self, **kwargs):
         | 
| 154 | 
            +
                    context = super().get_context_data(**kwargs)
         | 
| 155 | 
            +
                    '''
         | 
| 156 | 
            +
                    辞書のリストが帰ってくる
         | 
| 157 | 
            +
                    [{"id": 1, "price":500......}]
         | 
| 158 | 
            +
                    '''
         | 
| 159 | 
            +
                    all_list_price = Shopping_list.objects.all().values("id", "price")
         | 
| 160 | 
            +
                    sum_price = 0
         | 
| 161 | 
            +
                    for v in all_list_price:
         | 
| 162 | 
            +
                        price = v["price"]
         | 
| 163 | 
            +
                        sum_price += price
         | 
| 164 | 
            +
                    context['sum_price'] = sum_price
         | 
| 165 | 
            +
                    return context
         | 
| 166 | 
            +
             | 
| 167 | 
            +
                
         | 
| 168 | 
            +
            class Shopping_listCreateView(FormView):
         | 
| 169 | 
            +
                # model = Shopping_list
         | 
| 170 | 
            +
                # fields = ["name", "price"]
         | 
| 171 | 
            +
                form_class = Shopping_listFormSet
         | 
| 172 | 
            +
                template_name = "stockpile/shopping_list_form.html"
         | 
| 173 | 
            +
                success_url = "/stockpile/shopping_list/"
         | 
| 174 | 
            +
                
         | 
| 175 | 
            +
                def get_context_data(self, **kwargs):
         | 
| 176 | 
            +
                    context = super().get_context_data(**kwargs)
         | 
| 177 | 
            +
                    context['Week_menu'] = Week_menu.objects.all()
         | 
| 178 | 
            +
                    return context
         | 
| 179 | 
            +
                    
         | 
| 180 | 
            +
                def form_valid(self, form):
         | 
| 181 | 
            +
                    form.save()  # ここで保存
         | 
| 182 | 
            +
                    
         | 
| 183 | 
            +
                    for cleaned_data in form.cleaned_data:
         | 
| 184 | 
            +
                        Purchase_history.objects.create(name=cleaned_data["name"],
         | 
| 185 | 
            +
                        price=cleaned_data["price"])
         | 
| 186 | 
            +
             | 
| 187 | 
            +
                    return super().form_valid(form)
         | 
| 188 | 
            +
             | 
| 189 | 
            +
             | 
| 190 | 
            +
            def delete_shopping_list(request, shopping_list_id):
         | 
| 191 | 
            +
                shopping_list = get_object_or_404(Shopping_list, id=shopping_list_id)
         | 
| 192 | 
            +
                shopping_list.delete()
         | 
| 193 | 
            +
                shopping_list = Shopping_list.objects.all()
         | 
| 194 | 
            +
                context = {'object_list': shopping_list}
         | 
| 195 | 
            +
                return render(request, 'stockpile/shopping_list.html', context)
         | 
| 196 | 
            +
            ```
         | 
| 197 | 
            +
            ```python
         | 
| 198 | 
            +
            forms.py
         | 
| 199 | 
            +
             | 
| 200 | 
            +
            class Shopping_listForm(ModelForm):
         | 
| 201 | 
            +
             | 
| 202 | 
            +
                class Meta:
         | 
| 203 | 
            +
                    model = Shopping_list
         | 
| 204 | 
            +
                    fields = ("name", "price")
         | 
| 205 | 
            +
             | 
| 206 | 
            +
             | 
| 207 | 
            +
            Shopping_listFormSet = modelformset_factory(
         | 
| 208 | 
            +
                Shopping_list,
         | 
| 209 | 
            +
                form=Shopping_listForm,
         | 
| 210 | 
            +
                fields=("name", "price"),
         | 
| 211 | 
            +
                extra=3,
         | 
| 212 | 
            +
                can_delete=True
         | 
| 213 | 
            +
                )
         | 
| 214 | 
            +
             | 
| 215 | 
            +
            class Purchase_historyForm(ModelForm):
         | 
| 216 | 
            +
             | 
| 217 | 
            +
                class Meta:
         | 
| 218 | 
            +
                    model = Purchase_history
         | 
| 219 | 
            +
                    fields = ("name", "price")
         | 
| 220 | 
            +
            ```
         | 
