Django Image Uploads/Serving + Sending Emails in Django PDF

Title Django Image Uploads/Serving + Sending Emails in Django
Author Gautam Bose
Course Web Applications Development
Institution Carnegie Mellon University
Pages 3
File Size 42.2 KB
File Type PDF
Total Downloads 44
Total Views 155

Summary

These notes contains information from the lectures given for web applications. Specifically, they go over how to implement image uploads and serving on a django site, as well as how to use SMTP to prototype and test an email verification system.

These notes are essential to homework 3....


Description

Dj#ngo Im#ge Uplo#ds # upload_to will upload to the MEDIA_ROOT/addr-book-photos/ directory # Set MEDIA_ROOT environment variable in settings.py # Blank = true makes the field optional add models.ImageField(upload_to=“addr-book-photos”, blank = True)

In forms.py override the def+ult widget for the model form (if you w+nt to prevent deletion)

class (forms.ModelForm): exclude = ( , ) widgets = { ‘picture’ : forms.FileInput() }

in views.py def edit_entry(requeust): if rejjquest.method == “Get”: #Simply serve the page context = {‘form’:EntryForm()} return render(request, ‘simple-address-book/addentry.html’, context) new_entry = Entry(owner=request.user) #create the loaded form form = EntryForm(request.POST, request.FILES, instance=new_entry) ## NOTE: WE ADD REQUEST.FILES to the constructor. otherwise the action is exactly the same. install PIL

Dj#ngo Im#ge Serving when you go to displ+y + dyn+mic im+ge you need this:

In urls.py A route to some kind of ‘getphotoʼ url th+t h+s + regex for /get_photo/ th+t resolves to some function th+t returns the correct url for th+t photos id. in .html we displ+y the content with + lo+ded form, so we c+n simply check if

form.picture.v+lue is true to see if the model th+t lo+ded the form h+d + picture included.

In views.py from django.http import HttpResponse, Http404 # We can’t just use render() anmore from mimetypes import guess_type # Helper function to guess a MIME type from a file type at the get_photo url action: it uses @login_required def get_photo(request, id): entry = get_object_or_404(Entry, owner=request.user, id=id) # We make sure the requested user has permession to access the correct photo if not entry.picture: raise Http404 content_type = guess_type(entry.picture.name) return HttpResponse(entry.picture, mimetype=content_type)

Sending Em#ils In gener+l, sending em+il from within python is very e+sy. You c+n e+sily use SMTP +nd the MIMEText cl+ss to send em+ils. However, you must use +n extern+l SMTP server. we c+n use smtp.+ndrew.cmu.edu. however, this is b+d bec+use you would h+ve to put your +ndrew id in code ( could just use b+sh v+ri+bles??????? le+rn2code ) in settings.py

# To enable real email sending, uncomment and configure below # EMAIL_HOST = ‘smtp.andrew.cmu.edu’ # EMAIL_HOST_USER = ‘gbose’ # EMAIL_HOST_PASSWORd = ‘’ # EMAIL_USE_TLS = True for homework EMAIL_BAKCEND = ‘django.core.mail.backends.console.EmailBackend’

in views.py from django.core.mail import send_mail send_mail(subject = “verify your email address”, message = email-body, from_email = “Chalieewfwefoijwfoj” recipeint_list=([new_user.email])...


Similar Free PDFs