小编dan*_*ana的帖子

Django表单我无法保存图片文件

我有模特:

class OpenCv(models.Model):
    created_by = models.ForeignKey(User, blank=True)
    first_name = models.CharField(('first name'), max_length=30, blank=True)
    last_name = models.CharField(('last name'), max_length=30, blank=True)
    url = models.URLField(verify_exists=True)
    picture = models.ImageField(help_text=('Upload an image (max %s kilobytes)' %settings.MAX_PHOTO_UPLOAD_SIZE),upload_to='jakido/avatar',blank=True, null= True)
    bio = models.CharField(('bio'), max_length=180, blank=True)
    date_birth = models.DateField(blank=True,null=True)
    domain = models.CharField(('domain'), max_length=30, blank=True, choices = domain_choices)
    specialisation = models.CharField(('specialization'), max_length=30, blank=True)
    degree = models.CharField(('degree'), max_length=30, choices = degree_choices)
    year_last_degree = models.CharField(('year last degree'), max_length=30, blank=True,choices = year_last_degree_choices)
    lyceum = models.CharField(('lyceum'), max_length=30, blank=True)
    faculty = models.ForeignKey(Faculty, blank=True,null=True)
    references …
Run Code Online (Sandbox Code Playgroud)

database forms django image insert

1
推荐指数
1
解决办法
4600
查看次数

django从服务器下载文件到用户的机器,或在线阅读

我在我的服务器上上传了一些.doc和.txt文件,我想有两个选择: - 用户能够下载文件 - 用户能够在线阅读我已经阅读了一些代码对于下载功能,但它似乎不起作用.

我的代码:

def download_course(request, id):
    course = Courses.objects.get(pk = id)
    response = HttpResponse(mimetype='application/force-download')
    response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
    response['X-Sendfile'] = smart_str(/root/)
    return response

def save_course(request, classname):
   classroom = Classroom.objects.get(classname = classname)
   if request.method == 'POST':
        form = CoursesForm(request.POST, request.FILES)
        if form.is_valid():
           handle_uploaded_file(request.FILES['course'])
           new_obj = form.save(commit=False)
           new_obj.creator = request.user
           new_obj.classroom = classroom
           new_obj.save()
           return HttpResponseRedirect('.')    
   else:
           form = CoursesForm()     
   return render_to_response('courses/new_course.html', {
           'form': form,
           }, 
          context_instance=RequestContext(request)) 


def handle_uploaded_file(f):
    destination = open('root', 'wb+')
    for chunk in f.chunks(): …
Run Code Online (Sandbox Code Playgroud)

forms django download

1
推荐指数
1
解决办法
1万
查看次数

django上传文本文件不起作用

我正在尝试将文本文件上传到指定位置,但文件在根目录中上传空(不在我指定的位置).

在模型中:

course = models.FileField(help_text=('Upload a course (max %s kilobytes)' %settings.MAX_COURSE_UPLOAD_SIZE),upload_to='cfolder/',blank=True)
Run Code Online (Sandbox Code Playgroud)

形式:

def handle_uploaded_file(f):
    destination = open('Cfolder')
    for chunk in f.chunks():
        destination.write(chunk)       
    destination.close()
Run Code Online (Sandbox Code Playgroud)

在观点中:

def save_course(request, classname):
   classroom = Classroom.objects.get(classname = classname)
   if request.method == 'POST':
        form = CoursesForm(request.POST, request.FILES)
        if form.is_valid():
           handle_uploaded_file(request.FILES['course'])
           new_obj = form.save(commit=False)
           new_obj.creator = request.user
           new_obj.classroom = classroom
           new_obj.save()
           return HttpResponseRedirect('.')    
   else:
           form = CoursesForm()     
   return render_to_response('courses/new_course.html', {
           'form': form,
           }, 
          context_instance=RequestContext(request))  
Run Code Online (Sandbox Code Playgroud)

我想我的错误是在handle_uploaded_file方法中.我应该如何修改它才能正常工作?谢谢!

django upload text file

1
推荐指数
1
解决办法
758
查看次数

如何制作标签?

如果我有一个问答系统,并且我想添加一个标签功能,就每个问题我应该有一些标签,用逗号分隔(就像Stackoverflow一样):

  1. 我希望有一个单独的类模型,带有问题的外键
  2. 在表单中,我希望用户能够添加多个标签,用逗号分隔,并且在提交表单时,我希望将标签存储在表中:每个标签都注册

我应该在表单中使用什么,以便用逗号分隔的标签保存在数据库中,每个标签都注册?(便于搜索)

谢谢

database forms tags django

1
推荐指数
1
解决办法
267
查看次数

django从多个表连接查询集

如果我对多个表有查询,例如:

d = Relations.objects.filter(follow = request.user).filter(date_follow__lt = last_checked)
r = Reply.objects.filter(reply_to = request.user).filter(date_reply__lt = last_checked)
article = New.objects.filter(created_by = request.user)
vote = Vote.objects.filter(voted = article).filter(date__lt = last_checked)
Run Code Online (Sandbox Code Playgroud)

我希望显示按日期排序的所有结果(我的意思是不列出所有回复,然后是所有投票等).不知何故,我想在一个查询集中"加入所有这些结果".有可能吗?

django join django-queryset

0
推荐指数
1
解决办法
6237
查看次数

Django调用返回值的函数不起作用

我在调用从另一个函数返回结果的函数时遇到问题

为了说清楚,我的职责是:

def calculate_questions_vote(request):
    useranswer = Answer.objects.filter (answer_by = request.user)
    positive_votes = VoteUpAnswer.objects.filter(answer = useranswer)
    negative_votes = VoteDownAnswer.objects.filter(answer = useranswer)
    question_vote_rank = sum(positive_votes) - sum(negative_votes.count)
        return question_vote_rank

def calculate_replies(request):
    the_new = News.objects.filter(created_by = request.user)
    reply = Reply.objects.filter(reply_to = the_new)
    reply_rank = sum(reply)
        return reply_rank
Run Code Online (Sandbox Code Playgroud)

我想在另一个函数中调用它们,以便它可以返回一个值.我正在调用函数形式这样的另一个函数:

rank = calculate_questions_vote
Run Code Online (Sandbox Code Playgroud)

假设我现在只想显示函数calculate_questions_vote返回的值.当然,我将rank变量放在函数的上下文中.

我的问题是我的输出是:

<function calculate_questions_vote at 0x9420144>
Run Code Online (Sandbox Code Playgroud)

我怎样才能实际显示函数返回的值,而不是该字符串?

django return function call

0
推荐指数
2
解决办法
925
查看次数

kohana 3上传图像而不使用任何模块?

有没有人有一个在kohana 3中上传图像的例子,最终没有使用任何模块?

我使用https://github.com/kohana/image/tree/3.1%2Fmaster,但我的错误是:致命错误:未捕获Kohana_Exception [0]:已安装的GD不支持图像

有没有简单快捷的方法在基于kohana 3的网站上传文件?

谢谢!

php upload gd image kohana

0
推荐指数
1
解决办法
4551
查看次数

标签 统计

django ×6

forms ×3

database ×2

image ×2

upload ×2

call ×1

django-queryset ×1

download ×1

file ×1

function ×1

gd ×1

insert ×1

join ×1

kohana ×1

php ×1

return ×1

tags ×1

text ×1