Skip to content

Instantly share code, notes, and snippets.

@ishannla
Created December 27, 2017 00:46
Show Gist options
  • Save ishannla/5aa1f3255712471f07672fc279087af5 to your computer and use it in GitHub Desktop.
Save ishannla/5aa1f3255712471f07672fc279087af5 to your computer and use it in GitHub Desktop.
Web development
CREATING NEW PROJECT
To create the project, run $ "python django-admin.py startproject <name>", where <name> is the name of the project you wish to create.
CREATING NEW APP
1. To create a new application, run $ "python manage.py startapp <appname>", where <app- name> is the name of the application you wish to create.
2. Tell your Django project about the new application by adding it to the INSTALLED_APPS tuple in your project’s settings.py file.
3. In your project urls.py file, add a mapping to the application.
4. In your application’s directory, create a urls.py file to direct incoming URL strings to views.
5. In your application’s view.py, create the required views ensuring that they return a HttpRe-
sponse object.
CREATING + INTEGRATING TEMPLATE
1. First, create the template you wish to use and save it within the templates directory you specified in your project’s settings.py module. You may wish to use Django template variables (e.g. {{ variable_name }}) or template tags within your template. You’ll be able to replace these with whatever you like within the corresponding view.
2. Find or create a new view within an application’s views.py file.
3. Add your view specific logic (if you have any) to the view. For example, this may involve extracting data from a database and storing it within a list.
4. Within the view, construct a dictionary object which you can pass to the template engine as part of the template’s context.
5. Make use of the render() helper function to generate the rendered response. Ensure you reference the request, then the template file, followed by the context dictionary.
6. If you haven’t already done so, map the view to a URL by modifying your project’s urls.py file and the application specific urls.py file if you have one.
INTEGRATING STATIC MEDIA FILE
1. Take the static media file you wish to use and place it within your project’s static directory. This is the directory you specify in your project’s STATICFILES_DIRS list within settings.py.
2. Add a reference to the static media file to a template.For example, an image would be inserted into an HTML page through the use of the <img /> tag.
3. Remember to use the {% load static files %} and {% static "<filename>" %} commands within the template to access the static files. Replace <filename> with the path to the image or resource you wish to reference. Whenever you wish to refer to a static file, use the static template tag!
$ python manage.py migrate
$ python manage.py createsuperuser
$ python manage.py makemigrations rango # whenever making changes to models
$ python manage.py sqlmigrate rango 0001 # check underlying SQL of models
$ python manage.py shell # start instance of Python interpreter + load project settings
>>> from rango.models import Category
>>> print(Category.objects.all())
>>> c = Category(name="Test")
>>> c.save()
>>> print(Category.objects.all())
ADDING A MODEL
1. First, create your new model(s) in your Django application’s models.py file.
2. Update admin.py to include and register your new model(s).
3. Perform the migration $ "python manage.py makemigrations"
4. Apply the changes $ "python manage.py migrate". This will create the necessary infrastructure within the database for your new model(s).
5. Create/edit your population script for your new model(s).
DELETING DATABASE
1. migrate your database - this will set everything up in the new database. Ensure that your app is listed in the migrations that are committed. If it is not, run the makemigrations <appname> command, where <appname> is the name of your app.
2. Create a new administrative account with the createsuperuser command.
$ python manage.py makemigrations rango
$ python manage.py migrate
$ python populate_rango.py
All about getting links I guess?
REAL GOAL - UPDATING URL TO REFLECT THE PAGE THAT YOU ARE ON
$ django-admin.py startproject tango_with_django_project
$ python manage.py runserver
$ python manage.py runserver 0.0.0.0:5555
$ python manage.py startapp rango
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment