Notably, it's likely that you would use WhiteNoise on the server for your static files, however WhiteNoise explicitly does not support media files. Hosting this site in production would require a few additional steps. Īfter you submit a new post you'll be redirected back to the homepage and will see all the posts. That's it! Make sure your server is running and go to the page at. Within the view we specify the model, a form_class which we'll create next, the template_name, and finally a success_url which is what we want to happen after submission. We'll also import reverse_lazy to handle the redirect back to our homepage after the form has been submitted. We'll name our new view CreatePostView which will extend the built-in Django CreateView. That means creating a new page with a form. Now we can add a form so regular users, who wouldn't have access to the admin, can also add posts. Refresh the page if needed.Īnd voila! If you add additional posts with a title and image via the admin they will appear on the homepage. Ok, that's it! Make sure the server is running with the python manage.py runserver command and navigate to our homepage at. Note we also need to add the MEDIA_URL if settings are in DEBUG mode, otherwise we won't be able to view uploaded images locally. First at the project-level config/urls.py files we need to add imports for settings, include, and static. Our model is already done so that means diving into URL routes. I find it easiest to reason about this by going in order from models -> urls -> views -> template files.
The confusing thing about Django is that you often need 4 different but interconnected files for one webpage: models.py, urls.py, views.py, and a template html file. But let's take it a step further and display our posts which means urls.py, views.py, and template files. Ok, so at this point we're done with the basics. See! I told you that was what MEDIA_URL would do. If you look within the local media folder in your project you'll see under images there is now the djangopony.png image file. Upon "Save" you will be redirected to the Posts page where we can see all our posts.
You can download it here yourself if you like. You can add whatever you like but for this tutorial I'm making a post on the Django Pony mascot. It should redirect you to this page:Ĭlick on the "+ Add" link next to Posts. If you go to you'll be able to log in to the Django admin site. (insta) $ python manage.py createsuperuser (insta) $ python manage.py runserver