如何添加当前用户通用详细信息视图?

时间:2015-10-12 09:09:13

标签: django

我正在学习Django,方法是学习其他名为" portfolio"的其他应用教程。该代码位于http://www.lightbird.net/dbe/。我试图修改代码,以便当用户提交图片时,当前用户的名字可以保存为"创建者"在model.py中定义为图像模型的一部分。我很难做到这一点,因为我得到了" dpk"或者与" POST"中的参数数量相关的错误功能。有人可以帮忙吗?这是我的信息。我强调了添加的代码部分,它们不是原始代码的一部分。

models.py:

class Image(BaseModel):
    title       = CharField(max_length=60, blank=True, null=True)
    description = TextField(blank=True, null=True)
    image       = ImageField(upload_to="images/")
    thumbnail1  = ImageField(upload_to="images/", blank=True, null=True)
    thumbnail2  = ImageField(upload_to="images/", blank=True, null=True)

    width       = IntegerField(blank=True, null=True)
    height      = IntegerField(blank=True, null=True)
    hidden      = BooleanField(default=True)
    group       = ForeignKey(Group, related_name="images", blank=True)
    created     = DateTimeField(auto_now_add=True)
    creator     = models.ForeignKey(User, null=True, blank=True)
    ##########################################################
    def save(self,**kwargs):
      if kwargs.has_key('request') and self.creator is None:
            self.creator= kwargs['request'].creator
            super(Image, self).save(**kwargs)

forms.py:

class AddImageForm(f.ModelForm):
    class Meta:
        model   = Image
        exclude = "width height hidden group thumbnail1 thumbnail2    
    creator".split()
        attrs   = dict(cols=70, rows=2)
        widgets = dict( description=f.Textarea(attrs=attrs) )
    #####################################################################
    def save(self, commit=True , *args, **kwargs):
            m = super(AddImageForm, self).save(commit=False, *args, **kwargs)
            if m.creator is None and kwargs.has_key('request'):
              m.creator= kwargs['request'].creator
              m.save()

views.py:

class AddImages(DetailView, FormSetView):
    """Add images to a group view."""
    detail_model       = Group
    formset_model      = Image
    formset_form_class = AddImageForm
    template_name      = "add_images.html"
    extra              = 1
    ############################################################
    def post(self, request):
        if form.is_valid():
            form.save(request=request)
    ############################################################
    def process_form(self, form):
        form.instance.update( group=self.get_detail_object() )

    def get_success_url(self):
        return self.detail_absolute_url()

urls.py:

urlpatterns = patterns("portfolio.views",
    (r"^group/(?P<dpk>\d+)/(?P<show>\S+)/" , GroupView.as_view(), {}, 
     "group"),
    (r"^group/(?P<dpk>\d+)/"               , GroupView.as_view(), {}, 
     "group"),
    (r"^add-images/(?P<dpk>\d+)/"          , AddImages.as_view(), {}, 
    "add_images"),
    (r"^slideshow/(?P<dpk>\d+)/"           , SlideshowView.as_view(), {}, 
    "slideshow"),
    (r"^image/(?P<mfpk>\d+)/"              , ImageView.as_view(), {}, 
    "image"),
    (r"^image/"                            , ImageView.as_view(), {}, 
    "image"),
    (r""                                   , Main.as_view(), {},   
    "portfolio"),
)

1 个答案:

答案 0 :(得分:0)

您已覆盖视图中的curl_setopt($curl_handle, CURLOPT_USERAGENT, 'mozilla');方法仅接收请求,但您的网址会将post参数传递给它。

无论如何,你不应该覆盖dpk。你在那里的逻辑应该发生在post

(另外,你应该反思你是否真的需要在三个地方使用相同的逻辑。我会把所有这个代码放在form_valid中,而不是覆盖表单上的保存或模型。)