7 Critical Django Production Server Settings to Configure Before Going Live

Поділитися
Вставка
  • Опубліковано 14 жов 2024
  • Learn how to configure a Django production server with settings that ensure your Python website is secure before releasing it to the public. In addition to installing an SSL certificate and verifying the deployment with python manage.py check --deploy, these Django settings include:
    • SECRET_KEY
    • SESSION_COOKIE_SECURE
    • CSRF_COOKIE_SECURE
    • SECURE_SSL_REDIRECT
    • SECURE_HSTS_SECONDS
    • SECURE_HSTS_PRELOAD
    • SECURE_HSTS_INCLUDE_SUBDOMAINS
    • ALLOWED_HOSTS
    • DEBUG
    • STATIC_ROOT
    • STATIC_URL
    Check out my short domain name search engine that was featured in this video micro.domains
    0:57 Hide the secret key
    2:25 Install SSL certificate
    2:53 HTTPS settings
    3:35 HSTS settings
    4:45 ALLOWED_HOSTS
    5:15 Turn off debug
    6:10 Copy static files
    7:11 Run the deployment checklist
    Code snippets and more at tonyteaches.te...
    Check out my vlog channel ‪@TonyFlorida‬
    #django

КОМЕНТАРІ • 109

  • @markwollert565
    @markwollert565 3 роки тому +7

    Dude, I'm so grateful to you for the work you've done on all of your videos

  • @EvaLindner-v7v
    @EvaLindner-v7v 7 місяців тому +2

    Clear, concise, competent and to the point. Thank you!

  • @anselsolomon6270
    @anselsolomon6270 3 роки тому +3

    This is the first time am actually seeing someone on UA-cam shows with detail how to configure settings for Django production environment....i really appreciate you much on this....please can you make a video on how to deployment django with ability to send emails?? And with postgresql as the database???.....Thanks alot

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому

      Probably not postgres but I will consider a Django email tutorial :)

  • @madanielmadaniel
    @madanielmadaniel 2 роки тому +9

    1:32 - As for the secret key, also recommended to check the module 'decouple'. It does the same thing but in more elegant way.

    • @TonyTeachesTech
      @TonyTeachesTech  2 роки тому +1

      Thanks for the tip Daniel

    • @ChiragVora
      @ChiragVora 16 днів тому

      Or dotenv.
      Modules like these also help to hide other credentials like database name password and username etc

  • @krzysztofszumko9080
    @krzysztofszumko9080 3 роки тому +5

    Great Video! Straight to the point, no time wasted just awesome :). Keep up the good work.

  • @arnauadell4824
    @arnauadell4824 Рік тому

    Fucking amazing how quickly and concisely you could share all this information. And with very clear pronunciation too! Coming here in the future for any other django information I need.

  • @jvoynar5826
    @jvoynar5826 Рік тому +1

    Very nice, thank you!

  • @vectoralphaSec
    @vectoralphaSec 7 місяців тому +1

    Have you made a video about deploying a Django site to a PaaS?? I feel IaaS is too hard and complicated for beginners like me. I don't feel i need all that control and setup a full Linux server from scratch for a simple portfolio website.

  • @cheesecakelover2078
    @cheesecakelover2078 2 роки тому +1

    This was an amazing video, learnt quite a lot of things which turned out to be very important! Thank you so much :)

  • @KenoAlordiah
    @KenoAlordiah 3 роки тому

    Thank you so much for helping me figure this out. There were quite a number of settings I skipped on my production site settings.py file. Thanks once again.

  • @johnsolly
    @johnsolly 2 роки тому

    What a solid video. Thanks! I just implemented these changes on my site!

  • @shivamdubey4783
    @shivamdubey4783 2 роки тому

    Great tutorial sir do we have to change the file upload settings also

  • @TheLolcheater
    @TheLolcheater 2 роки тому

    Best Tutorial i have ever seen! Your Awsome!

  • @MadaMediaproduction
    @MadaMediaproduction 2 роки тому

    Great video thank you , i have issue with deploying project with Django corntab its works normally in local but in after production its not working anymore any hint to fix this issue

  • @pabloxD75
    @pabloxD75 3 роки тому +1

    What a great video with a great recommendations, thank you

  • @rsnor1425
    @rsnor1425 16 днів тому

    Good work!
    Please, make a tutorial to full deploy in Namecheap hosting... they have an strange way to serve static files for Admin panel

  • @joemusya2529
    @joemusya2529 2 роки тому

    Found this video in the right time💯💯🤗😊

  • @jcmbisa4241
    @jcmbisa4241 2 роки тому

    handsome, smart, and well rich too, man many thanks 😀😀

  • @NoajmIsMyName
    @NoajmIsMyName Рік тому

    Tony. Tony. Tony. You are awesome!

  • @agresiv0
    @agresiv0 2 роки тому

    Is it best practice to use the same database for development and production?

  • @Slickjitz
    @Slickjitz 3 роки тому

    Wow this was super helpful, thanks!

  • @nitensapkota8606
    @nitensapkota8606 2 роки тому

    Subscribed 🙌 can please make a video on how to access static files if you're hosting static files in digital ocean spaces ...i have hosted my static files in digital ocean spaces and I am not able to change anything on those files ..i have search online about it but they says AWS S3 is not mutable i have to download the files and edit the hosted file and then re-upload it which is not convenient ..

  • @enriqueuzcategui6057
    @enriqueuzcategui6057 2 роки тому

    Hey Tony! Thanks for the video! I have a problem with a Django deployment, how can I hide the "Server" header? It's something i am being asked to do but I don't seem to find a way to hide/remove it. Thank you!

  • @ingermanpaez8449
    @ingermanpaez8449 3 роки тому

    I am so grateful. God bless you son.

  • @anindyabhattacharya4878
    @anindyabhattacharya4878 3 роки тому

    precise content man.. good job.

  • @jvsydroyrakotonomenjanahar8324

    Thank you!

  • @navidazimi7816
    @navidazimi7816 3 роки тому

    Thank you so much this was so useful 🙏🏻
    Btw,I’m trying to serve my media files from a file hosting service that is connected to my main domain as a sub domain.
    I don’t know how should i tell Django to upload video files(inside media) to the other host..
    Any help will be appreciated🙏🏻
    Thank you so much🙏🏻

    • @TonyTeachesTech
      @TonyTeachesTech  2 роки тому

      Thanks! Sorry I don't have experience with Django uploads

  • @ricosun
    @ricosun 2 роки тому

    I was wondering what is the purpose of the secure key and why we need to hide it. Also what about the database connection string? Should we hide that as well ?

    • @TonyTeachesTech
      @TonyTeachesTech  2 роки тому

      You can see the official documentation for these values here docs.djangoproject.com/en/3.2/ref/settings/#secret-key

    • @ricosun
      @ricosun 2 роки тому

      @@TonyTeachesTech Cool Thx

  • @joelmiruni8660
    @joelmiruni8660 3 роки тому

    Brief and Straight to the point. Thanks. How could i solve this error: [Errno 99] Cannot assign requested address while registering users via email confirmation. Everything seems to work fine in development.

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому

      Hi Joel, I'm not familiar with that error. Sorry

  • @andresdemillos01
    @andresdemillos01 2 роки тому

    Un muy buen video felicitaciones por tu trabajo es un canal muy vacano de seguir (Y)

  • @mukibulhasan
    @mukibulhasan 3 роки тому

    nice information bro

  • @truefugazi
    @truefugazi 5 місяців тому

    So I am having issue, I hosted my project with render with a DB SQLite and when ever I update a listing in my admin after a few hours it resets/removes my listing I can’t seem to find my way around this please help

  • @jayeshkarkare9334
    @jayeshkarkare9334 3 роки тому

    Grt work bro,thnx a lot

  • @thebewerter5730
    @thebewerter5730 3 роки тому

    Cool, thanks!

  • @ethanroman6883
    @ethanroman6883 3 роки тому

    Great video, keep going

  • @mahdidaoud
    @mahdidaoud 3 роки тому

    Can you please explain the SECURE_PROXY_SSL_HEADER setting what it does for the django project and how to set it

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому

      I have not used this one before, but here is the docs on it docs.djangoproject.com/en/3.2/ref/settings/#secure-proxy-ssl-header

  • @codepen2035
    @codepen2035 2 роки тому

    Brother when I enable debug=False Static file not working? Please help me. I am using VPS hosing with centos 7

  • @viveksharmapec7637
    @viveksharmapec7637 3 роки тому +1

    thanks bdy

  • @toluwanimiadeyemo8099
    @toluwanimiadeyemo8099 3 роки тому

    Great video! Thanks
    But how do I hide my database details, email details
    What about using .env file?

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому +1

      Yes you can use an env file that's above of the root of your website

  • @G-3-A-R-Z
    @G-3-A-R-Z 3 роки тому

    Why not a secret key in an .env? Coming from the JS world just a few weeks ago. Noticed they used a similar system.

  • @MrSiamtea
    @MrSiamtea 3 роки тому +1

    I have a problem in reading env variables from ubuntu server. I can hide the credentials to environment just fine in devlopment as well as production. But the trick is my django app cannot retrieve that env keys in production. I'm missing something. I am searching for solutions, still searching for it. I'm not a great programmer, but I don't think this is hiding the secret key. It just relocate the key to a text file which is in the same directory as the app itself. How is that hiding? I'm confused.

  • @henrim9348
    @henrim9348 3 роки тому

    Great video. Thanks a lot. What production environment do you deploy to? GCP?AWS?

  • @bronaxcorps4243
    @bronaxcorps4243 Рік тому

    Hey how can I separate my development database from my production database??

  • @Goodforyouliving
    @Goodforyouliving 3 роки тому

    When I change DEBUG=False all my media files (images) are not showing up? What is the correct way to make this work ?

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому

      I don't know why that is happening

    • @BlueBrendan2000
      @BlueBrendan2000 3 роки тому

      You've probably figured this out by now but I'll just leave this for anyone who may stumble upon this. I ran into this issue and spent hours looking for a fix
      1. I wrote the src attribute for all my image elements with a hardcoded path (src="/images/somewhere/something.png"). In the official docs, it says to use the static template tag {% static "image path" %}. The static module can contain EITHER a string or a template variable but NOT both, so if you need that functionality then you'll have to create a custom template filter to append the strings together (the add filter didn't work for me). If you don't, then you can skip to step 2. There's an answer describing how this is done on stackoverflow (appending template variable in static TAG). An example of this is if the directory you need is /static/images/files/filename.png, and filename.png is stored in template variable {{ filename }}, the code you'll need in your html file will look like this
      {% with 'images/files/'|customfiltername:filename as variable_name %}
      {% endwith %}
      customfiltername is what performs the concatenation of 'images/files' with the contents of filename (again, this filter does not exist and needs to be written by you). As a note, make sure you include {% load static %} and {% load *name of the file that contains your custom template filter %} at the top of each file that contains an image
      2. I messed up the static paths in my settings dot py file: there's STATIC_URL (the directory of the main static folder), STATICFILES_DIR (provides directory for static files in individual apps that are outside the main static folder), and STATIC_ROOT (used only when DEBUG=False). STATIC_ROOT exists because in production, Django doesn't serve static files from individual apps the way it does in production, so it bundles them all together in a single location. STATICFILES_DIR and STATIC_ROOT can't have the same name, so I used a folder called staticfiles to be used solely for production (static is the name of my main static folder used in development). To set your STATIC_ROOT directory, choose a directory name (I chose staticfiles), add the setting to your settings file, create an empty folder of the name you chose in the root folder of your project, and run the command "python manage dot py collectstatic". This will automatically traverse all the static files in your project and add them to the folder you chose in STATIC_ROOT
      Once I made these changes (added STATIC_ROOT to the settings, created the corresponding folder, ran the collectstatic command to populate it with files, and changed the source of all images in my project to use the {% static %} template tag), the images finally showed up as expected with DEBUG=False. I really hope this helps!

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому

      @@BlueBrendan2000 This will surely be helpful for others. Thanks!

  • @kunalkashyap100
    @kunalkashyap100 3 роки тому

    If I make these changes while my website is running on the server, do I need to restart the uwsgi service after making these changes or not?

  • @Alex-hp9no
    @Alex-hp9no 2 роки тому

    hi, Im having trouble with media files, when I turn debug=True, then all of user added images shows on the page, and when i turn it False, it says the image is not found..
    usual configurations in settings.py:
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR, "static/")
    MEDIA_URL = "/media/"
    MEDIA_ROOT = os.path.join(BASE_DIR, "media/")
    and also urls:
    url.. = [
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    pls help me out here

    • @TonyTeachesTech
      @TonyTeachesTech  2 роки тому

      Appreciate the code snippet. I would look at the URL of the image that's not found and compare that to the path on your filesystem

  • @xpert9001
    @xpert9001 3 роки тому

    Nice.

  • @rounakchourasia2030
    @rounakchourasia2030 3 роки тому

    That intro after that calm voice! I was on 80% volume! :(

  • @manuponot5093
    @manuponot5093 3 роки тому

    Hello I have my website on production on Digital Ocean. My settings.py file has the Debug = False. I ran systemclt restart gunicorn command but nothing changed. However, I see the the page as if it was in Debug Mode. Please, how I can fix this issue ?

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому

      Could this be a browser caching issue? Have you tried incognito or a guest window?

    • @manuponot5093
      @manuponot5093 3 роки тому

      @@TonyTeachesTech My problem is solved thanks

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому

      @@manuponot5093 What that the issue?

    • @manuponot5093
      @manuponot5093 3 роки тому

      ​@@TonyTeachesTech It was my fault I have created a config file local_settings.py and didn't remeber that it exists and it was Debug = True inside

  • @dscreats
    @dscreats 3 роки тому

    i got this error how to resolve with open (os.path.join(BASE_DIR, 'SECRET_KEY.text')) as f: SECRET_KEY = f.red().strip()
    AttributeError: '_io.TextIOWrapper' object has no attribute 'red'

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому

      No attribute 'red'? That would be a syntax error... red -> read

  • @saudzubedi
    @saudzubedi 3 роки тому

    My site just doesn't work after adding SECURE_SSL_REDIRECT=True, this is the error: "You're accessing the development server over HTTPS, but it only supports HTTP." Some help would be nice :)

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому +1

      Are you launching your site using the runserver command? If so, that's your problem. These settings are for production servers ua-cam.com/video/ZpR1W-NWnp4/v-deo.html

  • @alexi_space
    @alexi_space 2 роки тому

    after set SECURE_SSL_REDIRECT = True I always get: True page isnt redirecting properly ...

  • @enzopascucci9933
    @enzopascucci9933 3 роки тому

    After setting hsts setting i couldn't been able to run on localhost to changes anything

    • @TonyTeachesTech
      @TonyTeachesTech  3 роки тому

      You'll need to clear HSTS settings for your website from your browser really-simple-ssl.com/knowledge-base/clear-hsts-browser/

    • @enzopascucci9933
      @enzopascucci9933 3 роки тому

      @@TonyTeachesTech it didn't worked, but worked with the package django-sslserver

  • @YadavDheeraj2000
    @YadavDheeraj2000 2 роки тому

    Abe Bhai kya bta rha

  • @SamSam-e5c
    @SamSam-e5c 5 місяців тому

    horrible sizer inducing intro. more so at 2x speed. video content is good though.

  • @BikiniGyat
    @BikiniGyat 3 роки тому

    Awesome! Thanks a lot!

  • @balajishetty
    @balajishetty Рік тому

    Excellent tutorial. Can you give expert lectures in our college on Django . Whatsapp Me

  • @daniellloyd5584
    @daniellloyd5584 2 роки тому

    Thank you!