Posts tagged: django

All posts with the tag "django"

4 posts latest post 2022-10-09
Publishing rhythm
Oct 2022 | 1 posts
npx create-react-app todoreact import React,{useState,useEffect} from 'react'; import './App.css'; function App() { const [data,setData]=useState([]); const [newName,setNewName]=useState([]); const getData=()=>{ fetch('/api' ,{ headers : { 'Content-Type': 'application/json', 'Accept': 'application/json' } } ) .then(function(response){ return response.json(); }) .then(function(myJson) { setData(myJson) }); } useEffect(()=>{ getData() },[]) const addItem= async () => { const rawResponse = await fetch('/api/add/', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({"name": newName}) }); const content = await rawResponse; console.log(content); getData() } return ( <div className="App"> { data && data.length>0 && data.map((item)=><p>{item.id}{item.priority}{item.name}<button>raise priority</button></p>) } <input type='text' value={newName} onChange={(e) => (setNewName(e.target.value))} /> <button onClick={addItem} >add item</button> </div> ); } export default App;
In my adventure to learn django, I want to be able to setup REST api’s to feed into dynamic front end sites. Potentially sites running react under the hood. [1] Install # [2] To get started lets open up a todo app that I created with django-admin startproject todo. pip install djangorestframework Install APP # [3] Now we need to declare rest_framwork as an INSTALLED_APP. INSTALLED_APPS = [ ... "rest_framework", ... ] create the api app # [4] Next I will create all the files that I need to get the api running. mkdir api touch api/__init__.py api/serializers.py api/urls.py api/views.py [5] base/models.py # [6] I already have the following model from last time I was playing with django. It will suffice as it is not the focus of what I am learning for now. Note the name of the model class is singular, this is becuase django will automatically pluralize it in places like the admin panel, and you would end up with Itemss. from django.db import models # Create your models here. class Item(models.Model): name = models.CharField(max_length=200) created = models.DateTimeField(auto_now_add=True) def __str__(self): return f"{self.priority} {self.name}" Next I will m...
My next step into django made me realize that I do not have access to the admin panel, turns out that I need to create a cuper user first. [1] Run Migrations # [2] Right away when trying to setup the superuser I ran into this issue django.db.utils.OperationalError: no such table: auth_user Back to the tutorial [3] tells me that I need to run migrations to setup some tables for the INSTALLED_APPS, django.contrib.admin being one of them. python manage.py migrate [4] yes I am still running remote on from my chromebook. python manage.py createsuperuser [5] The super user has been created. [6] CSRF FAILURE # [7] My next issue trying to run off of a separate domain was a cross site request forgery error. Since this is a valid domain that we are hosting the app from we need to tell Django that this is safe. We can do this again in the settings.py, but this time the variable we need is not there out of the box and we need to add it. CSRF_TRUSTED_ORIGINS = ['https://localhost.waylonwalker.com'] I made it!! # [8] And we are in, and welcomed for the first time with this django admin panel. [9] Remote Hosting # [10] You might find these settings helpful as well if yo...
I am continuing my journey into django, but today I am not at my workstation. I am ssh’d in remotely from a chromebook. I am fully outside of my network, so I can’t access it by localhost, or it’s ip. I do have cloudflared tunnel installed and dns setup to a localhost.waylonwalker.com. Settings # [1] I found this in settings.py and yolo, it worked first try. I am in from my remote location, and even have auth taken care of thanks to cloudflare. I am really hoping to learn how to setup my own auth with django as this is one of the things that I could really use in my toolbelt. ALLOWED_HOSTS = ['localhost.waylonwalker.com'] [2] References: [1]: #settings [2]: https://stable-diffusion.waylonwalker.com/000321.3422093952.webp
I have no experience in django, and in my exploration to become a better python developer I am dipping my toe into one of the most polished and widely used web frameworks Django to so that I can better understand it and become a better python developer. If you found this at all helpful make sure you check out the django tutorial [1] [2] install django # [3] The first thing I need to do is render out a template to start the project. For this I need the django-admin cli. To get this I am going the route of pipx it will be installed globally on my system in it’s own virtual environment that I don’t have to manage. This will be useful only for using startproject as far as I know. pipx install django django-admin startproject try_django cd try_django [4] Make a venv # [5] Once I have the project I need a venv for all of django and all of my dependencies I might need for the project. I have really been diggin hatch lately, and it has a one line “make a virtual environment [6] and manage it for me” command. hatch shell [7] If hatch is a bit bleeding edge for you, or it has died out by the time you read this. The ol trusty venv will likely stand the test of time, this is w...