1
を見つけることができません。私のurls.py
では、私は次のようにありますDjangoは、私は、次のファイル構造を持つサイトを持っている画像
urlpatterns = [
url(r'^profile/(?P<profile_id>[0-9]+)/$', views.Details, name = "account"),
url(r'^update/', views.Update_Profile, name = "update"),
] + static(settings.STATIC_URL, document_root = settings.STATIC_ROOT) +\
static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
テンプレートprofile.html
は、次のようになります。
{% extends "base.html" %}
{% load profiles_extras %}
{% block content %}
{% load staticfiles %}
<div class="container-fluid">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<!-- Store Information -->
<div class="col-lg-6">
<div class="panel panel-default">
<div class="panel-heading">
<h2>Store Information</h2>
</div>
<div class="center-block">
<!-- Change picture form -->
<form method="post" action="{% url 'update' %}" class="hidden form-inline" id="picture-change-form" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group">
<input type="file" class="form-control" name="picture" accept="image/*">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
{% if profile.logo %}
<img src="{{ profile.logo.url }}" class="img-responsive" id="logo" alt="Store Logo">
{% else %}
<img src="{% static 'no-image.png' %}" class="img-responsive" id="logo" alt="Store Logo">
{% endif %}
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
プロフィール画像がアップロードされると、profiles.views.py
でこの関数が呼び出されます。しかし
def update_logo(self, value):
self.logo = value
self.save()
私は写真をアップロードする際に、my_image.png
それはuser_1/my_image.png
として起動し、ファイルが見つからなかったため、サーバーは404を返します。
@login_required
def Update_Profile(request):
update_profile = Profile.objects.get(user = request.user)
has_updated = False
if ('picture' in request.FILES):
update_profile.update_logo(request.FILES['picture'])
return redirect('profiles.views.Details', profile_id = update_profile.id)
順番にこの機能はupdate_logo
profiles.models.py
に呼び出し、 。私はここで間違って何をしていますか?
私もそれを試みましたが、うーん、それはうまくいきませんでした。 '/ user_1 /'はどのように定義するのですか? – Woody1193
'MEDIA_URL'を'/media/'に変更すると、アップロードされた画像は'/media/user_1/'で利用可能になり、'/user_1/'urlを定義する必要はありません。 –
'Profile'モデルからすべてのデータを削除しようとしてください –