{"id":19262008,"url":"https://github.com/hacksu/django-examples","last_synced_at":"2026-02-27T03:00:57.496Z","repository":{"id":5400395,"uuid":"6590027","full_name":"hacksu/django-examples","owner":"hacksu","description":"A tutorial to get started with Django.","archived":false,"fork":false,"pushed_at":"2012-11-09T01:37:58.000Z","size":120,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-20T12:51:43.595Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hacksu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-11-08T02:30:34.000Z","updated_at":"2014-02-04T12:09:05.000Z","dependencies_parsed_at":"2022-07-06T15:34:24.736Z","dependency_job_id":null,"html_url":"https://github.com/hacksu/django-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/hacksu/django-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2Fdjango-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2Fdjango-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2Fdjango-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2Fdjango-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hacksu","download_url":"https://codeload.github.com/hacksu/django-examples/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hacksu%2Fdjango-examples/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29883111,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T23:51:21.483Z","status":"online","status_checked_at":"2026-02-27T02:00:06.759Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T19:29:20.860Z","updated_at":"2026-02-27T03:00:57.478Z","avatar_url":"https://github.com/hacksu.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introduction to Django\n\n## Installing Django\n\n### Mac/Unix\n1. Download [Django-1.4.2.tar.gz](https://www.djangoproject.com/download/1.4.2/tarball/)\n2. Run the following commands in terminal:\n\n```\ntar xzvf Django-1.4.2.tar.gz\ncd Django-1.4.2\nsudo python setup.py install\n```\n\n### PC\n1. Download and install Python 2.7 if you dont have it. (http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi)\n2. Download Django (https://github.com/django/django/zipball/master)\n3. Copy django folder into C:\\Python27\\Lib\\site-packages\\\n4. Right click on “My Computer” and select Properties.\n5. Go to Advanced, then Environment Variables at the bottom.\n6. Edit “Path” and at the end add the following:\n\n```\n;C:\\Python27;C\\Python27\\scripts;C:\\Python27\\Lib\\site-packages\\django\\bin\n```\n\n\n##### Test to find out if it was a great success!\nIn cmd (you can find this program from the start menu), type\n\n```\npython\n```\n\nand press enter. The python shell should appear. Enter\n\n```\nimport django\n```\n\nand then\n\n```\ndjango.get_version()\n```\n\nThis should return\n\n```\n1.6\n```\n\nYAY\n\nNote: exit() will get you out of the shell\n\n## Once you have Django installed\n\n### Creating your first project\n\nin terminal enter:\n\n    django-admin.py startproject mysite\n\nThis will create a my site directory in your current directory. In the my site directory, the following has been created:\n\n    mysite/\n        manage.py\n        mysite/\n            __init__.py\n            settings.py\n            urls.py\n            wsgi.py\n\nTo run your Django server, enter the following in your my site folder:\n\n    python manage.py runserver \n\nNote, by default it is hosted at http://127.0.0.1:8000/\n\nTake a look at the \"Welcome to Django\" page :)\n\nTo change the port, you can pass it when you call the run run server command:\n\n    python manage.py runserver 8080\n\nSo that is pretty sweet, you just ran your first Django server! :)\n\n### Setting up the database\n\nOpen up mysite/settings.py. Find the DATABASE object, and we will use sqlite3 in this example.\n\nLets make the following changes to settings.py (2 options):\n\n```python\n'ENGINE' : 'django.db.backends.sqlite3',\n'NAME' : '/Users/username/some/path/database.db', \n```\n\nOR\n\n```python\nimport os\n\nSITE_ROOT = os.path.dirname(os.path.realpath(__file__))\n...\n'ENGINE' : 'django.db.backends.sqlite3',\n'NAME' : os.path.join(SITE_ROOT, 'database.db'),\n```\n\nNow that we have the database setting complete, lets sync the database:\n\n    python manage.py syncdb\n\n### Creating the Models\n\nNow, lets create an app. A project can have many apps.\n\n    python manage.py startapp hacksu\n\nThe following directory will be created:\n\n    hacksu/\n        __init__.py\n        models.py\n        tests.py\n        views.py\n\nLets give our app some models, open up the models.py file and add the following:\n\n```python\nfrom django.db import models\n\nclass Member(models.Model): \n    name = models.CharField(max_length=200)\n    email = models.CharField(max_length=200)\n    join_date = models.DateTimeField()\n\nclass Project(models.Model):\n    name = models.CharField(max_length=200) \n    url = models.CharField(max_length=200)\n    description = models.CharField(max_length=200)\n    members = models.ManyToManyField(Member)\n```\n\nCool, so you have created two models, now to link them. In settings.py, let's add 'hacksu' to INSTALLED_APPS in settings.py\n\nAwesome. So now lets tell Django to include our app. \n\n    python manage.py sql hacksu\n\nNow you will see the two create tables commands. That is super awesome. You never need to create a database manually again =)\n\nTo create those new model tables in your database, run:\n\n    python manage.py syncdb    \n\nLets play with the python shell:\n\n    python manage.py shell\n\nLets create a member:\n\n    \u003e\u003e\u003e from hacksu.models import Member\n    \u003e\u003e\u003e Member.objects.all()\n    []\n\n    \u003e\u003e\u003e from django.utils import timezone\n    \u003e\u003e\u003e m = Member(name=\"Dawe\", email=\"some@one.com\", join_date=timezone.now())\n    \u003e\u003e m.save()\n    \u003e\u003e Member.objects.all()\n    [\u003cMember: Member object\u003e]\n    \n    # That is not cool!\n\nLets fix our models, so that they give us human readable info. In hacksu/models.py\n```python\nclass Member(models.Model):\n    # ...\n    def __unicode__(self):\n        return self.name\n\nclass Project(models.Model):\n    # ...\n    def __unicode__(self):\n        return self.name\n```\n\n\nLets hop back into the django shell, and type:\n\n    \u003e\u003e\u003e from hacksu.models import Member\n\n    \u003e\u003e\u003e Member.objects.all()\n\n    [\u003cMember: Dawe\u003e]\n\n### Admin Features\n\nSo now we have done too much backend stuff. Let us look at the admin features. There are three steps needed:\n\n1. Open up settings.py, and in INSTALLED_APPS, and uncomment 'django.contrib.admin'  \n2. Run 'pyton manage.py syncdb'\n3. In mysite/urls.py, uncomment the following 3 lines:\n    \n```python\nfrom django.contrib import admin\nadmin.autodiscover()\n...\nurl(r'^admin/', include(admin.site.urls)),\n```\n\nNow, run the server, by entering the following in terminal:\n\n    python manage.py runserver\n\nVisit: http://127.0.0.1:8000/admin/ and enter the admin username and password.\n\nThat is awesome, right. But you will see, Auth and Sites. Would you like to see your data models there too?\n\nOk, so lets open /hacksu/admin.py and add the following:\n\n```python\nfrom hacksu.models import Member, Project\nfrom django.contrib import admin\n\nadmin.site.register(Member)\nadmin.site.register(Project)\n```\n\nThis tells Django to add your models to the admin interface. \n\nTo customize the template look and feel, go [here](https://docs.djangoproject.com/en/1.4/intro/tutorial02/#customize-the-admin-look-and-feel)\n\n### Designing the URL's\n\nIn mysite/urls.py add the following: \n\n```python\n#...\nurlpatterns += patterns('hacksu.views',\n    url(r'^$', 'index'),\n)\n```\n\nThat tells the server, that when someone requests root, they will be forwarded to index of our hacksu views. We need to add the functionailty there, so lets open hacksu/views.py\n\n```python\nfrom django.shortcuts import render_to_response\nfrom hacksu.models import Member, Project\n\ndef index(request):\n    members = Member.objects.all()\n    projects = Project.objects.all()\n    return render_to_response('hacksu/index.html', {'members': members, 'projects': projects})\n```\n\nWe also need to create the html file, and link it up. So lets open up settings.py and set the TEMPLATE_DIRS\n    \n```python    \nTEMPLATE_DIRS = (\n    os.path.join(SITE_ROOT, 'templates'),\n)\n```\n\nLets create a templates folder in mysite, and create an index.html file inside there. \n\nThen inside that html file we can access our model objects and can embed python code :)\n\n```html\n\u003c!DOCTYPE html\u003e\n    \u003chead\u003e\n        \u003ctitle\u003ehacKSU Members\u003c/title\u003e\n    \u003c/head\u003e\n    \u003cbody\u003e\n        {% for member in members %}\n            \u003cp\u003e{{ member.name }}\u003c/p\u003e\n            \u003cp\u003e{{ member.email }}\u003c/p\u003e\n            \u003cp\u003e{{ member.join_date }}\u003c/p\u003e\n        {% endfor %}\n\n        {% if not members %}\n            No members!\n        {% endif %}\n    \u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Get your app on Heroku!\n\nDownload and install Heroku, then launch your app from terminal:\n\n1. heroku create\n2. git push heroku master\n3. heroku open\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhacksu%2Fdjango-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhacksu%2Fdjango-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhacksu%2Fdjango-examples/lists"}