Classified Ads + Pictures

In this assignment, you will expand your classified ads web site to add functionality equivalent to:

The primary additions from the previous milestone are to add an image to each ad and keep all the features from the previous version of the application.

You will build this application by borrowing parts and pieces from the code that runs

and combining them into a single application.

Important Note: If you find you have a problem saving files in the PythonAnywhere system using their browser-based editor, you might need to turn off your ad blocker for this site - weird but true.

Adding Pictures to the Ads Application

In this section, you will pull bits and pieces of the pics sample application into your ads application to add support for an optional single picture per ad.

(1) Add this to your ads/, taking inspiration from dj4e-samples/pics/

class Ad(models.Model) :

    # Picture
    picture = models.BinaryField(null=True, blank=True, editable=True)
    content_type = models.CharField(max_length=256, null=True, blank=True,
        help_text='The MIMEType of the file')

Do not include the entire Pic model. Of course do the migrations once you have modified the model.

(2) Copy in the pics/ as well as pics/ Edit the pics/ and change only the following four lines:

from pics.models import Pic
from pics.humanize import naturalsize
        model = Pic
        fields = ['title', 'text', 'picture']


from ads.models import Ad
from ads.humanize import naturalsize
        model = Ad
        fields = ['title', 'text', 'picture', 'price']

Leave the other bits of ads/ alone. Only change the name of the model in the above two lines.

(3) Take a look at pics/ and adapt the patterns in PicCreateView and PicUpdateView and replace the code for AdCreateView and AdUpdateView in ads/ These new views don't inherit from because they manage the owner column in the get() and post() methods.

(4) Alter your templates/ads/ad_form.html by looking through pics/templates/pics/form.html. Make sure to add the JavaScript bits at the end and add enctype="multipart/form-data" and the id attribute to the form tag.

(5) Alter the templates/ads/ad_detail.html template by looking through pics/templates/pics/detail.html and to add code to include the image in the output if there is an image associated with the ad. Make sure not to lose the price field in your UI. If you don't see the price field in your UI it is likely a mistake in your

(6) Add an ad_picture route to your based on the pics_picture route from pics/

path('ad_picture/<int:pk>', views.stream_file, name='ad_picture'),

(5) Add the stream_file() view from pics/ and adapt appropriately.

Test to make sure you can upload, view, and update pictures with your ads.

Manual Testing

It is always a good idea to manually test your application before submitting it for grading. Here are a set of manual test steps:

Do Some or All of the Challenges

You will have to finish these eventually - so you might as well work on them now. And they are fun.

(1) Make yourself a gravatar at - it is super easy and you will see your avatar when you log in in your application and elsewhere with gravatar enabled apps. The gravatar can be anything you like - it does not have to be a picture of you. The gravatar is associated with an email address so make sure to give an email address to the user you create with createsuperuser.

(2) Change your home/static/favicon.ico to a favicon of your own making. I made my favicon at - it might not change instantly after you update the favicon because they are cached extensively. Probably the best way to test is to go right to the favicon url after you update the file and press 'Refresh' and/or switch browsers. Sometimes the browser caching is "too effective" on a favicon so to force a real reload to check if the new favicon is really being served you can add a GET parameter to the URL to force it to be re-retrieved:

Change the x value to something else if you want to test over and over.

(3) Make social login work. Take a look at, copy it into mysite/mysite/ and go through the process on github to get your client ID and secret. The documentation is in comments of the file. Also take a look at dj4e-samples/ and make sure that the "Switch to social login" code is correct and at the end of your mysite/mysite/

You can register two applications with github - one on localhost and one on PythonAnywhere. If you are using github login on localhost - make sure that you register instead of http://localhost:8000/ and use that in your browser to test your site. If you use localhost, you probably will get the The redirect_uri MUST match the registered callback URL for this application. error message when you use social login.