Reputation: 633
why I can not upload the images?? The urls.py: see the
url(r'^things,,,
from django.conf.urls import include, url
from django.contrib import admin
from collection import views
from django.views.generic import TemplateView
from django.views.generic import (TemplateView,RedirectView,)
from collection.backends import MyRegistrationView
from django.contrib.sitemaps.views import sitemap
from collection.sitemap import (
ThingSitemap,
StaticSitemap,
HomepageSitemap,
)
sitemaps = {
'things': ThingSitemap,
'static': StaticSitemap,
'homepage': HomepageSitemap,
}
from django.contrib.auth.views import (
password_reset,
password_reset_done,
password_reset_confirm,
password_reset_complete,
)
urlpatterns = [
url(r'^$', views.index, name='home'),
url(r'^about/$',TemplateView.as_view(template_name='about.html'),name='about'),
url(r'^contact/$',views.contact, name='contact'),
url(r'^things/$', RedirectView.as_view(pattern_name='browse', permanent=True)),
url(r'^things/(?P<slug>[-\w]+)/$', views.thing_detail,name='thing_detail'),
url(r'^things/(?P<slug>[-\w]+)/edit/$', views.edit_thing,name='edit_thing'),
url(r'^things/(?P<slug>[-\w]+)/edit/images/$', views.edit_thing_uploads,name='edit_thing_uploads'),
url(r'^browse/$', RedirectView.as_view(pattern_name='browse', permanent=True)),
url(r'^browse/name/$',views.browse_by_name, name='browse'),
url(r'^browse/name/(?P<initial>[-\w]+)/$',views.browse_by_name, name='browse_by_name'),
url(r'^accounts/password/reset/$', password_reset,{'template_name':'registration/password_reset_form.html'},name="password_reset"),
url(r'^accounts/password/reset/done/$', password_reset_done,{'template_name':'registration/password_reset_done.html'},name="password_reset_done"),
url(r'^accounts/password/reset/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', password_reset_confirm,{'template_name':'registration/password_reset_confirm.html'},name="password_reset_confirm"),
url(r'^accounts/password/done/$', password_reset_complete,{'template_name':'registration/password_reset_complete.html'},name="password_reset_complete"),
url(r'^accounts/register/$',MyRegistrationView.as_view(),name='registration_register'),
url(r'^accounts/create_thing/$',views.create_thing,name='registration_register'),
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^sitemap\.xml$', sitemap, {'sitemaps':sitemaps},name='django.contrib.sitemaps.views.sitemap'),
url(r'^admin/', include(admin.site.urls)),
]
from django.conf import settings
if settings.DEBUG:
urlpatterns += [
url(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root': settings.MEDIA_ROOT,}),
]
the views.py:
See the def edit_thing_uploads
from django.shortcuts import render,redirect
from django.contrib.auth.decorators import login_required
from django.http import Http404
from collection.forms import ThingForm,ContactForm,ThingUploadForm
from collection.models import Thing,Upload
from django.template.loader import get_template
from django.core.mail import EmailMessage
from django.template import Context
def index(request):
things = Thing.objects.all()
return render(request, 'index.html', {
'things':things,
})
def thing_detail(request, slug):
thing = Thing.objects.get(slug=slug)
social_accounts = thing.social_accounts.all()
uploads = thing.uploads.all()
return render(request, 'things/thing_detail.html',{
'thing':thing,
'social_accounts': social_accounts,
'uploads': uploads,
})
@login_required
def edit_thing(request, slug):
thing = Thing.objects.get(slug=slug)
if thing.user != request.user:
raise Http404
form_class = ThingForm
if request.method == 'POST':
form = form_class(data=request.POST, instance=thing)
if form.is_valid():
form.save()
return redirect('thing_detail', slug=thing.slug)
else:
form = form_class(instance=thing)
return render(request, 'things/edit_thing.html',{'thing':thing,'form':form,})
@login_required
def edit_thing_uploads(request, slug):
thing = Thing.objects.get(slug=slug)
if thing.user != request.user:
raise Http404
form_class = ThingUploadForm
if request.method == 'POST':
form = form_class(data=request.POST,files=request.FILES, instance=thing)
if form.is_valid():
Upload.objects.create(
image=form.cleaned_data['image'],
thing=thing,
)
return redirect('edit_thing_uploads', slug=thing.slug)
else:
form = form_class(instance=thing)
uploads = thing.uploads.all()
return render(request, 'things/edit_thing_uploads.html', {
'thing': thing,
'form': form,
'uploads': uploads,
})
def create_thing(request):
form_class = ThingForm
if request.method == 'POST':
form = form_class(request.POST)
if form.is_valid():
thing = form.save(commit=False)
thing.user = request.user
thing.slug = slugify(thing.name)
thing.save()
return redirect('thing_detail', slug=thing.slug)
else:
form = form_class()
return render(request, 'things/create_thing.html', {'form':form,})
def browse_by_name(request, initial=None):
if initial:
things = Thing.objects.filter(name__istartswith=initial)
things = things.order_by('name')
else:
things = Thing.objects.all().order_by('name')
return render(request, 'search/search.html', {'things':things,'initial':initial,})
def contact(request):
form_class = ContactForm
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
contact_name = form.cleaned_data['contact_name']
contact_email = form.cleaned_data['contact_email']
form_content = form.cleaned_data['content']
template = get_template('contact_template.txt')
context = Context({
'contact_name':contact_name,
'contact_email':contact_email,
'form_content':form_content,
})
content = template.render(context)
email = EmailMessage(
'New contact form submission',content,
'Your website <[email protected]>',['[email protected]'],
headers = {'Reply-To':contact_email }
)
email.send()
return redirect('contact')
return render(request, 'contact.html', {'form': form_class, })
The forms.py:
from django import forms
class ThingForm(ModelForm):
class Meta:
model = Thing
fields = ('name','description',)
class ContactForm(forms.Form):
contact_name = forms.CharField(required=True)
contact_email = forms.EmailField(required=True)
content = forms.CharField(
required=True,
widget=forms.Textarea
)
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
self.fields['contact_name'].label = "Your name:"
self.fields['contact_email'].label = "Your email:"
self.fields['content'].label = "What do you want to say?"
class ThingUploadForm(ModelForm):
class Meta:
model = Upload
fields = ('image',)
the edit_thing.html:
{% extends 'layouts/base.html' %}
{% block title %}
Edit {{ thing.name }} - {{ block.super }}
{% endblock %}
{% block content %}
<h1>Edit "{{ thing.name }}"</h1>
<a href="{% url 'edit_thing_uploads' slug=thing.slug %}">Edit images</a>
<form role="form" action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% endblock %}
the edit_thing_uploads.html:
{% extends 'layouts/base.html' %}
{% block title %}
Edit {{ thing.name }}'s Images - {{ block.super }}
{% endblock %}
{% block content %}
<h1>Edit {{ thing.name }}'s Images</h1>
<h2>Uploaded images</h2>
{% for upload in uploads %}
<img src="{{ uploads.image.url }}" alt="" />
{% endfor %}
<h2>Uploads a new image</h2>
<form role="form" action="" method="post" enctype="multipart/form\-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="submit" />
</form>
{% endblock %}
when I click in http://127.0.0.1:8000/things/another-things/edit/ is ok
however when I click the Edit images link to submit upload images it said no file has been choice?? I can not upload the images
Upvotes: 3
Views: 102
Reputation: 633
I got to know why ,,because I typos <form role="form" action="" method="post" enctype="multipart/form-data"> ---> <form role="form" action="" method="post" enctype="multipart/form\-data">
in edit_thing_uploads.html
Upvotes: 0
Reputation: 53
You have to check the file permissions of
edit_thing_uploads.html
, make sure the file is accessible. because the path has been correct. and Django can't see it.
Upvotes: 1