Django Codecademy Terms

¡Supera tus tareas y exámenes ahora con Quizwiz!

django how to activate app

1. django-admin startproject projectname 2. cd into that project folder and run a migration so that the migration errors go away. run the command python3 manage.py migrate. 3. We run a test to see if there were any errors in the migration. run the command python3 manage.py runserver 0.0.0.0:40001 or whatever severnumber you want. 4. After the server is started, navigate to localhost in the browser, and you should see a default splash page from Django. or it will run in visual studio code. 5. Use Django's startapp command with the manage.py script and create an app named #nameofapp 6. The startapp command will create files and folders for the new app. We'll have to add our new app, appname to our list of installed apps for our Django project to be aware of it. In the code editor, open up settings.py inside appnamefolder/appnamefolder. Find the list named INSTALLED_APPS and add the config file for appname by including "appname.apps.appnameConfig" to the list. By doing this we are letting our project know about our app. 7. For this project we created an HTML template file that would be used to display the fortune in the browser. So we create a folder inside of the app and we create a file named templates. Within that newly created file you would create a folder named after your app to namespace the template file. 8. Next we create an HTML file inside of the deepest folder that has been namespaced after the app name. In that file we make it an HTML file and we paste our HTML code into it. Be sure to save it when you have finished pasting. 9. We next need to make a view function that sends our app to the client when the page is requested. Inside the random fortune app open views.py. Inside the random fortune app open views.py. Define a new function named fortune() that takes a single parameter request. In fortune return the render function with two arguments, the request and our template as a string. the fortune function will look simiilat to def myView(request): return (request, "my/App/myTemplate.html") We create a fortune.html when called. We need to tell Django which URL we want to direct this fucntion. First we create the URL conf for the randomfortune app by create a file named urls.py inside the app directory. 10. we create a file called urls.py inside the app directory. and we import path module from django.urls. we import the functions from views.py. the code looks like this from django.urls import path from . import views 11. After importing the necessary modules into urls.py, we will create a list of patterns for Django to match URLs against. Create a list called urlpatterns and set it as a blank list. url patterns = [] 12. urlpatterns = [ path("", views.functionName)]

INSTALLED_APPS

INSTALLED_APPS which contains the apps that make up the Django project,

urls.py.

Inside are the URL declarations for this Django project, or a "table of contents" for the Django project. Remember earlier when we said that Django goes down a list of patterns to match a URL? This is that list! "like a mailman trying to find an address." codecademy

manage.py

Inside the project root folder is a Python file, manage.py, that contains a collection of useful functions used to administer the project. This file performs the same actions as django-admin but is set specifically to the project. We can do a number of things with it, such as starting a server. A development server can be started by using manage.py and providing the runserver command. This command must be run in the root directory, the same directory where manage.py is located. By default, Django will start a development server with port 8000, but an alternate port can be provided as an option. python3 manage.py runserver <port number> When you start the database its possible you will get an error message about migrations. You need to migrate the database changes first. python3 manage.py migrate migrates things over.

view

More simply put, a view is a class or function that processes a request and sends a response back.

URL CONF Path in Django

On the internet, every page needs its own URL because each URL displays unique information. In Django, we can use something called a URLconf, for URL configuration. This module is a set of patterns that Django will try to match the requested URL to find the correct view. After the import statements is a list of patterns called urlpatterns, which contain the routes to each view function. Each route is provided as a path() object that has three arguments: the URL route as a string, the name of the function of the view, and an optional name used to refer to the view..

namespace

That is to place our HTML pages inside a directory that has the same name as your app within the templates/ directory. What is the purpose of creating a nested template folder structure? That's correct! Django will choose the first template it finds whose name matches, and if you had a template with the same name in a different application, Django would be unable to distinguish between them.

render()

The render() function will do the work of loading the template and provide the contexts when they are passed as arguments.

path function

Use the path function inside of urlpatterns to establish connections between the app and django.

We've been talking about Django projects and apps for a while, but what exactly are apps? How are apps different from a project?

We've been talking about Django projects and apps for a while, but what exactly are apps? How are apps different from a project? Well, a Django app is a submodule to a project, that contains the code for a specific feature. In the submodule, we'll find things like: a models.py file, a migration directory, and other files and directories related to the application. Django apps should be self-sufficient and in theory, can be picked up and placed in another project without any modification. Meanwhile A Django project refers to the entire code base and its parts. The Django project folder holds manage.py and the other module that includes settings.py.

How to get a list of commands

django-admin help

Django Admin

django-admin startproject projectname

Codecademy Habits

highlight as you read. with each project step, do it first, than look at the video for the answer. Write down things in your own words and break up parts of the lessoninto quizlet. take the slides and pictures as well and look at them.

A migration is a pending database change.

python3 manage.py migrate A migration is a pending database change. As we saw in settings.py, by default, Django will have some apps installed. Some of these default apps, for example, the admin interface, use the database and the migrations must be applied to the SQLite database. Whenever we make changes to the model of the database, we must apply the changes by running python3 manage.py migrate. After applying the migration, when we run the server our errors are gone. By applying our migration, we have access to the admin app! The admin app comes pre-installed and can be navigated to since it has its URL provided in urls.py we saw earlier. At the moment there aren't any admin users but we can still visit localhost/admin to see the admin login page.

settings.py

settings.py is a Python file that contains configurations that we'll be editing throughout the development of our project. INSTALLED_APPS list is found here. Also DATABASES is found here. After running the startproject command, our settings.py should contain:


Conjuntos de estudio relacionados

Unit 4 Vocabulary Personal Finance

View Set

Chapter 28- Hematologic Function

View Set

ATI - Priority Setting Frameworks Beginning & Advanced Test

View Set