我最近遇到了这个关于 Flask 文件上传的问题和答案/sf/answers/3144859021/。
这工作得很好,直到我再次上传同名的图像。它没有改变图像或覆盖它。
我的问题是,如果用户上传同名图像,我们是否可以显示错误消息或自动将名称更改为其他名称。
对于自动更改名称,我研究过,可以通过resolve_conflict来完成,但我不明白如何实现它。
我有以下代码:
return [p.to_dict() for p in points]
Run Code Online (Sandbox Code Playgroud)
我将其更改为仅打印每第n行:
n = 100
count = 0
output = []
for p in points:
if (count % n == 0):
output.append(p.to_dict())
count += 1
return output
Run Code Online (Sandbox Code Playgroud)
是否有更多的pythonic方式来写这个,以获得相同的结果?
我正在创建一个多用户的费用提交系统。
为了解决这个问题,有两个模型:Claim和Journey。用户创建一个声明,每个声明可以有多个旅程。我已经对代码片段做了一个要点,因为它很长。
在这个片段中,我成功地:
ClaimListView.get_queryset过滤,因此登录的用户只能看到自己的声明列表。ClaimCreateView.form_valid提交表单时设置了正确的用户。ClaimDetailView.get_queryset由当前用户进行过滤。如果有人尝试通过该 url 获取其他用户的索赔详细信息,他们会收到 404(完美!)JourneyListViewJourneyDetailView- 如果未授权,则再次返回 404 :D但是,当我JourneyCreateView通过 URL 访问时,下拉框claim仍然显示其他用户的声明。
我应该如何过滤类中的用户JourneyCreateView,以便该claim字段仅显示分配给当前用户的声明?
我得到的最接近的解决方案是这个答案,它建议重写__init__其中的函数,这JourneyForm将使我得到这个:
class JourneyForm(forms.ModelForm):
class Meta:
model = Journey
fields = ['date', 'distance','claim']
def __init__(self,alloweduser,*args,**kwargs):
super (JourneyForm,self ).__init__(self,*args,**kwargs) # populates the post
self.fields['claim'].queryset = Claim.objects.filter(tech_id=alloweduser)
Run Code Online (Sandbox Code Playgroud)
但是我不确定如何alloweduser从中传递JourneyCreateView,或者更重要的是,获取此类中的当前用户。
form_valid在这种情况下没有任何用处,因为我试图在提交表单之前获取用户。