我正在制作图像'上行系统。 但是我收到了一个错误, / accounts / upload_save /的ValueError 对于带有基数为10的int(),文字无效:'' 。 我认为这个错误是int类型值不在参数中。
我的追溯显示
photo_obj = Post.objects.get(id=photo_id)
错了。 但是get()可以在int类型值中,所以我无法修复我的错误。 我该如何解决?
我在views.py,
中写道@require_POST
def regist_save(request):
form = RegisterForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
context = {
'user': request.user,
}
return redirect('profile')
context = {
'form': form,
}
return render(request, 'registration/accounts/regist.html', context)
def photo(request):
d = {
'photos': Post.objects.all(),
}
return render(request, 'registration/accounts/profile.html', d)
def upload(request, p_id):
d = {
'p_id': p_id,
}
return render(request, 'registration/accounts/profile.html', d)
def upload_save(request):
photo_id = request.POST.get("p_id", "")
photo_obj = Post.objects.get(id=photo_id)
files = request.FILES.getlist("files[]")
photo_obj.image1 = files[0]
photo_obj.image2 = files[1]
photo_obj.image3 = files[2]
photo_obj.save()
return redirect("registration/accounts/profile.html")
在urls.py中
from django.conf.urls import url
from . import views
from django.contrib.auth.views import login, logout
urlpatterns = [
url(r'^regist/$', views.regist,name='regist' ),
url(r'^regist_save/$', views.regist_save, name='regist_save'),
url(r'^profile/$', views.profile, name='profile'),
url(r'^upload/(?P<p_id>\d+)/$', views.upload, name='upload'),
url(r'^upload_save/$', views.upload_save, name='upload_save'),
]
profile.html中的
{% extends "registration/accounts/base.html" %}
{% block content %}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>UPLOAD</title>
</head>
<body>
user.username: {{ user.username }}<hr>
user.is_staff: {{ user.is_staff }}<hr>
user.is_active: {{ user.is_active }}<hr>
user.last_login: {{ user.last_login }}<hr>
user.date_joined: {{ user.date_joined }}
<div class="container">
<form action="{% url 'accounts:upload_save' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="files[]" multiple>
<input type="hidden" value="{{ p_id }}" name="p_id">
<input type="submit" value="Upload">
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</body>
</html>
{% endblock %}
答案 0 :(得分:2)
photo_id = request.POST.get("p_id", "")
此POST请求中没有p_id
,因此photo_id
设置为空字符串''
photo_obj = Post.objects.get(id=photo_id)
然后您尝试使用此空字符串获取photo_obj
,因此它会告诉您ValueError: invalid literal for int() with base 10: ''