<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: shlokabhalgat</title>
    <description>The latest articles on DEV Community by shlokabhalgat (@shlokabhalgat).</description>
    <link>https://dev.to/shlokabhalgat</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F823958%2Fa5981fb7-e2f0-46fc-8856-727bb8c2976f.jpeg</url>
      <title>DEV Community: shlokabhalgat</title>
      <link>https://dev.to/shlokabhalgat</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shlokabhalgat"/>
    <language>en</language>
    <item>
      <title>Querying Data from MongoDB using GraphQL in Django: Get-Go (1)</title>
      <dc:creator>shlokabhalgat</dc:creator>
      <pubDate>Tue, 26 Apr 2022 14:47:31 +0000</pubDate>
      <link>https://dev.to/shlokabhalgat/querying-data-from-mongodb-using-graphql-in-django-get-go-1-p7c</link>
      <guid>https://dev.to/shlokabhalgat/querying-data-from-mongodb-using-graphql-in-django-get-go-1-p7c</guid>
      <description>&lt;p&gt;The ease of MongoDB combined with the power of GraphQL: I cannot think of a better combination than this! I will be curating a series of articles for working on GraphQL + MongoDB + Django. This is the first part of that series.&lt;/p&gt;

&lt;p&gt;In this article, you will learn how to-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;import data into MongoDB using a CSV file&lt;/li&gt;
&lt;li&gt;install GraphQL and other related Django libraries&lt;/li&gt;
&lt;li&gt;pull data from MongoDB to Django&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  From .csv to MongoDB Collection
&lt;/h3&gt;

&lt;p&gt;Run this code directly in your terminal (not mongod console) to import a CSV file from your local. We are using mongoimport which is a tool/command that enables us to import any JSON, CSV to TSV file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;mongoimport&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="n"&gt;vendors&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="n"&gt;vendors&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="n"&gt;vendorlist&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;csv&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;headerline&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;/p&gt;

&lt;p&gt;-d: database name&lt;/p&gt;

&lt;p&gt;-c: collection name&lt;/p&gt;

&lt;p&gt;-headerline: used to keep the headers&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing GraphQL in Django
&lt;/h3&gt;

&lt;p&gt;Yet another saga of versioning mismatch. I have already played with them so you don't have to 🙂&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TQnqerD0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2qkjzhl34jlbzl5g95i.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TQnqerD0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2qkjzhl34jlbzl5g95i.jpeg" alt="Version table" width="880" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Below are the errors that were causing a lot of confusion regarding the versions-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zd3POurg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5x26psqse8gaiemsajo.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zd3POurg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5x26psqse8gaiemsajo.jpeg" alt="Error in terminal" width="880" height="75"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Error when the version of graphene-django was 3.0b7 which was latest but it couldn’t import the get_default_backend package&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dSNnwEap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kbwjj05kttnpk467p26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dSNnwEap--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5kbwjj05kttnpk467p26.png" alt="library name" width="880" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Commands to install each library/package/driver&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;graphene
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;graphene&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mf"&gt;2.1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;graphql-core
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;graphql&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mf"&gt;2.3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;pymongo
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python3&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;pymongo&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mf"&gt;3.12&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;graphene-django
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="s"&gt;"graphene-django&amp;gt;=2.0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 Don’t forget to add ‘graphene-django’ and ‘graphene’ to your settings.py file&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting a MongoDB collection’s data to Django project
&lt;/h3&gt;

&lt;p&gt;Import the Document class from  mongoengine library and subsequently import the fields that are required.&lt;/p&gt;

&lt;p&gt;💡 Your can check the datatype of the field in MongoDB in MongoDB Atlas console and then import the field in your Django project. Example- The ID field in a document id always an ObjectIDField.&lt;/p&gt;

&lt;p&gt;I have a MongoDB document vendors and the class that is accessing the document is VendorData. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Og7daZa5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tejslo5r8ggo2dxzp53q.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Og7daZa5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tejslo5r8ggo2dxzp53q.jpeg" alt="importing mongodb document in django" width="880" height="885"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This was the first part of the series of MongoDB+Django+GraphQL tutorials. I will explain more about writing queries in Django and GraphQL in the later parts.&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>django</category>
      <category>mongodb</category>
      <category>python</category>
    </item>
    <item>
      <title>Connecting Django with MongoDB: All the Versioning Shenanigans Dissected (2022 edition)</title>
      <dc:creator>shlokabhalgat</dc:creator>
      <pubDate>Thu, 24 Mar 2022 11:21:32 +0000</pubDate>
      <link>https://dev.to/shlokabhalgat/connecting-django-with-mongodb-all-the-versioning-shenanigans-dissected-2022-edition-13km</link>
      <guid>https://dev.to/shlokabhalgat/connecting-django-with-mongodb-all-the-versioning-shenanigans-dissected-2022-edition-13km</guid>
      <description>&lt;p&gt;Trust me, I have tried connecting Django with MongoDB using pymongo, mongoengine, and djongo finally. Although MongoDB’s website gives in-depth and stepwise instructions to do it, there are some versioning issues that need to be sorted out before aiming for that “Huge” (Mongo literally translates to huge 😛) Database to do wonders for your project.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Here I am assuming you are using the latest Django version 2.2.26 and Python version 3.6+&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Skip Step 1 if you already have MongoDB installed. Please check the version once to have a better understanding of the 2 drivers that we will use to connect with Django.&lt;/p&gt;

&lt;p&gt;To check MongoDB version use-&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mongod --version&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1
&lt;/h3&gt;

&lt;p&gt;Install MongoDB on local from terminal using the following command-&lt;/p&gt;

&lt;p&gt;brew install &lt;a href="mailto:mongodb-community@5.0"&gt;mongodb-community@5.0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For Windows, you need to get the installer and follow the steps as given  &lt;a href="https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-windows/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2
&lt;/h3&gt;

&lt;p&gt;Download MongoDB Compass from &lt;a href="https://www.mongodb.com/try/download/compass" rel="noopener noreferrer"&gt;here&lt;/a&gt;. It gives a user-friendly interface like SQLWorkbench and you can even run a MongoDB cluster for free from there. The application looks something like this-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i20q6jo49cgpq21pcwn.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9i20q6jo49cgpq21pcwn.jpeg" alt="MongoDB Compass Interface"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3
&lt;/h3&gt;

&lt;p&gt;Connecting to a Django Project&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Using Djongo&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Install djongo through terminal-&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install djongo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Coming to actually connecting MongoDB with your Django project using djongo, all we need to do is add the following to the settings.py file-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    DATABASES = {
          'default': {
            'ENGINE': 'djongo',
            'NAME': 'YOUR_DATABASE_NAME',
            'HOST': 'localhost',
            'PORT': '27017'
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Using MongoEngine&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To connect MongoDB with your Django project using mongoengine, you need to add the following to the settings.py file-&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    _MONGODB_USER = ""
    _MONGODB_PASSWD = ""
    _MONGODB_HOST = "localhost"
    _MONGODB_NAME = "YOUR_DATABASE_NAME"
    _MONGODB_PORT = 27017
    _MONGODB_DATABASE_HOST = "mongodb://%s:%s@%s/%s" % (
        _MONGODB_USER,
        _MONGODB_PASSWD,
        _MONGODB_HOST,
        _MONGODB_NAME,
    )

    mongoengine.connect(_MONGODB_NAME, host=_MONGODB_HOST, port=_MONGODB_PORT)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that you can import it in your models.py file and use it-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep3j477c9cg38fxcuctd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep3j477c9cg38fxcuctd.png" alt="Mongoengine usage in models.py file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Using PyMongo&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The latest pymongo version is 4.0.2 which is incompatible with the latest mongoDB v5.0. I got this error after which I downgraded the pymongo version.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pymongo.errors.ConfigurationError: Server at xxxx:xx reports wire version 5, but this version of PyMongo requires at least 6 (MongoDB 3.6).
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;pymongo v 3.12.1&lt;/p&gt;

&lt;p&gt;MongoDb v 5.0.5&lt;/p&gt;

&lt;p&gt;Source - &lt;a href="https://pypi.org/project/pymongo/" rel="noopener noreferrer"&gt;pypi.org&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because of versioning shenanigans I was turned off. &lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Key Takeaways&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;I would highly recommend using mongoengine for its ease to access collections through models.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Djongo is very easy to connect with Django. ( I had tried to connect all three and somehow pymongo’s version incompatibility wouldn’t allow me to connect with djongo)&lt;/p&gt;

&lt;p&gt;Apparently, Djongo has dependencies on pymongo and sqlparse so we need to get the version right for pymongo after installing Djongo-&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1pekbvdg08qwo3qtbfs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1pekbvdg08qwo3qtbfs.png" alt="Djongo + sqlparse + Pymongo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if you go up and check pymongo’s versioning issues, you will know that MongoDB v5.0 is not compatible with PyMongo’s v 4.0&lt;/p&gt;

&lt;p&gt;To downgrade pymongo’s version run this command-&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 -m pip install pymongo==3.12.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;Hence using the following combinations did wonders for me -

&lt;ul&gt;
&lt;li&gt;Djongo v 1.3.6&lt;/li&gt;
&lt;li&gt;Pymongo v 3.12.1&lt;/li&gt;
&lt;li&gt;MongoDB v 5.0.5&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>mongodb</category>
      <category>django</category>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>Using static files in Django</title>
      <dc:creator>shlokabhalgat</dc:creator>
      <pubDate>Fri, 04 Mar 2022 11:25:46 +0000</pubDate>
      <link>https://dev.to/shlokabhalgat/using-static-files-in-django-2e2n</link>
      <guid>https://dev.to/shlokabhalgat/using-static-files-in-django-2e2n</guid>
      <description>&lt;p&gt;Static files not loading in Django? The approach to making static files load on the browser and making sure it is correctly summoned in the HTML file is extremely precarious. &lt;/p&gt;

&lt;p&gt;In this blog, I will explain why the static files are not loading as well as the necessary points to consider to call for them correctly-&lt;/p&gt;

&lt;p&gt;Static files contain any media files-images, javascript, CSS, and likewise. Check step by step if the requirements are fulfilled properly-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make sure that &lt;strong&gt;django.contrib.staticfiles&lt;/strong&gt;  is included in INSTALLED_APPS in settings.py&lt;/li&gt;
&lt;li&gt;Check if you have defined STATIC_URL in &lt;a href="http://settings.py"&gt;settings.py&lt;/a&gt; as follows-
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;STATIC_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'static/'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Check if your static files are in a folder called “static” in the root directory. Root directory is the project name directory where &lt;a href="http://manage.py"&gt;manage.py&lt;/a&gt; file is located.&lt;/li&gt;
&lt;li&gt;Adding static template path to the HTML file-
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
{% load static %}
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"{% static 'my_app/example.jpg' %}"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"My image"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Check if the STATICFILES_DIR is added below STATIC_URL in settings.py
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;STATICFILES_DIRS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;BASE_DIR&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="s"&gt;"static"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;'/&amp;lt;project_name&amp;gt;/&amp;lt;folder_name&amp;gt;/static/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;If none of the above steps work, add this path additionally to the STATICFILES_DIRS. So the final code will look something like this-
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;STATIC_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'static/'&lt;/span&gt;
&lt;span class="n"&gt;STATICFILES_DIRS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="n"&gt;BASE_DIR&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="s"&gt;"static"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;STATICFILES_DIRS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BASE_DIR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"static"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Additionally, please check the enclosures to add static files for css and javascript&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;link rel="stylesheet" href="{% static 'css/style.css' %}"&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;lt;script src="{% static 'js/main.js' %}"&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Most Efficient Way to Create a Django Project and Django App</title>
      <dc:creator>shlokabhalgat</dc:creator>
      <pubDate>Wed, 02 Mar 2022 06:06:46 +0000</pubDate>
      <link>https://dev.to/shlokabhalgat/most-efficient-way-to-create-a-django-project-and-django-app-h4n</link>
      <guid>https://dev.to/shlokabhalgat/most-efficient-way-to-create-a-django-project-and-django-app-h4n</guid>
      <description>&lt;p&gt;In this blog, I will explain and illustrate with pictures/commands the most effective ways to create a Django project from scratch.&lt;/p&gt;

&lt;p&gt;Source- &lt;a href="https://docs.djangoproject.com/en/"&gt;Django Documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First things first, because of the various dependencies in Python version and OS restrictions, I feel that the best way to start a Django project is using the virtual environment. Some of the problems that I faced while installing Django are &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;pip is not supported in the latest macOS instead we need to use &lt;strong&gt;pip3&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;New macOS has inbuilt Python version 2.7.18 and if we try to install any later versions there are a lot of changes to be made in the path(.zshrc file for macOS) &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Anaconda is also an option to solve the python versioning problem but again we have to install Anaconda  for that purpose. Thus, creating a virtual environment to start the project is the best way to it. &lt;/p&gt;

&lt;p&gt;💡 I am using &lt;strong&gt;Python version 3.8.9&lt;/strong&gt; with &lt;strong&gt;Django version 2.2.26.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to create a Django project:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Set up the virtual environment
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python3&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;venv&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Activate the virtual environment:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nb"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;activate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Install Django in the virtual environment:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;django&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create the root directory with the same name as the project name with &lt;a href="http://manage.py"&gt;manage.py&lt;/a&gt; file that is required for running all further commands while working models and Django server. It will also create another folder with the project name that is the actual Python package for your project which contains the &lt;strong&gt;init&lt;/strong&gt;.py, settings.py, urls.py, asgi.py, wsgi.py.&lt;/p&gt;

&lt;p&gt;💡 You can check the project structure by going to /Users//django-env&lt;/p&gt;

&lt;p&gt;Alright then, lets check if the project works on localhost. To do the same run the following command-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;runserver&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have completed the above steps, you can run the following command to start an application which will contain the models, templates folder and views for building the webapp.&lt;/p&gt;

&lt;h3&gt;
  
  
  Steps to create a Django App
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;startapp&lt;/span&gt; &lt;span class="n"&gt;myapp&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a directory structure as shown below-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myapp/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 All apps that are created will be listed under the root directory of the Django &lt;strong&gt;project&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can check the entire project on &lt;a href="https://github.com/shlokabhalgat/DjangoProjectVenv.git"&gt;GitHub&lt;/a&gt; or start building your application on top of it directly.&lt;/p&gt;

</description>
      <category>django</category>
      <category>beginners</category>
      <category>python</category>
    </item>
  </channel>
</rss>
