Category

Django

Category

Create a model class Contact(models.Model): name = models.CharField(max_length=100) email = models.EmailField(max_length=100) subject = models.CharField(max_length=100) message = models.TextField() mobile = models.IntegerField() created_date = models.DateField(auto_now_add=True) Load View from views.py from .models import Contact def indexPageView(request): contact = Contact.objects.all() return render(request, “index.html”,{‘contact’: contact}) Print data in a template {% for contact in contact %} <h1>{{contact.name}}</h1> {% endfor %}

First, create Model class Contact(models.Model): name = models.CharField(max_length=100) email = models.EmailField(max_length=100) subject = models.CharField(max_length=100) message = models.TextField() mobile = models.IntegerField() created_date = models.DateField(auto_now_add=True) Import model within the admin from django.contrib import admin from .models import Contact # Register your models here. admin.site.register(Contact) Create superuser python manage.py createsuperuser Enter your desired username and press enter. Username: admin You will then be prompted for your desired email address: Email address: admin@example.com Start server python manage.py runserver

Create a model field within models.py file class Contact(models.Model): name = models.CharField(max_length=100) email = models.EmailField(max_length=100) subject = models.CharField(max_length=100) message = models.TextField() mobile = models.IntegerField(max_length=180) created_on = models.DateTimeField(auto_now_add=True) Associate with settings.py INSTALLED_APPS = [ ‘contact.apps.ContactConfig’, ] Run migrations command python manage.py makemigrations Check Migration python manage.py showmigrations python manage.py migrate

Create a model class Page(): id: int name: str Import to View from .models import Page def indexPageView(request): page = Page() page.name =”home” page1 = Page() page1.name = “about” page2 = Page() page2.name = “contact” page = [page, page1, page2] return render(request, “index.html”, {‘page’: page}) Now add for loop to index.html {% for page in page %} <h1>{{ page.name }}</h1> {% endfor %}

There are 3 main things we need to do: set STATIC_ROOT in settings.py run python manage.py collectstatic (or python3.5 or python3.6 as appropriate) Example: python manage.py collectstatic set up a Static Files entry on the PythonAnywhere Web tab. we can also customise STATIC_URL, if we want to use a static URL prefix other than /static/ STATIC_ROOT = os.path.join(BASE_DIR, ‘assets’) Set url.py to load template from .views import indexPageView urlpatterns = [ path(”, indexPageView, name=’index’)] Now,…

What is a Web Framework? A web framework is a code library which helps you to build a flexible, scalable, and maintainable, dynamic website, web app, and web services. Different web frameworks are Zend for PHP, Ruby on Rails for Ruby, etc. What is Django? Django is a web development framework for Python which offers a standard method for fast and effective website development. It helps you to assists in building and maintaining quality web…

How To Install Django 2 In Mac First, we need to install python3 in our system -> brew install python3 Now check the version of python -> python3 Install pip package management system. -> sudo easy_install pip Install virtualenv for python. -> sudo pip install virtualenv -> virtualenv ven -> cd venv -> source bin/activate Install Django 2 -> pip install django==2.0.3 check django2 version -> python -m django –version Create the Django project with…