Django Basic Project Startup Code

DJANGO


Render , redirect

from django.shortcuts import render
from django.http import HttpResponseRedirect


def home(request):
    if request.user.is_authenticated:
        zstocks = wlist_V2_m.objects.all().order_by('stocklist_id')[:250]
        for zline in zstocks:
            break
        return render(request, 'wlist/wlist_V2.html', {'zstocks':zstocks ,})
    else:
        return render(request , 'wlist/403.html')

def restart_bg(request):
    #Restart all background jobs
    del_bg_jobs()
    update_nse_data_all(repeat=14400) ; 
    update_tech_indicators(repeat=7200)
    update_nsedata_wlist(repeat=120) ; update_nsedata_tradebook(repeat=120)
    return HttpResponseRedirect('/wlist/system/')

 

Starter Template

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <h1>Hello, world!</h1>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>

 

Extended Template

{% extends "codebase/layout_5.html" %}

{% block content %}

<form action="/codebase/addcode/" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.media }}
<div class="container">
     {{ form.as_p }}
      <input type="submit" value="Save">
  
</div>
   
</form>
{% endblock %}

 {% block scripts %} 
<script>
    var zinput = document.querySelectorAll("input,select");
    for (i = 0; i <= zinput.length - 1; i++) {
        try {
            document.getElementById(zinput[i].id).className = "form-control";
        } catch (err) {
        }
    }
</script>

{% endblock %}

 

CURD Template

 Create

def addstocknews(request):
    # Add stock to watchlist and link portfolio
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = stock_news_f(request.POST)
        # check whether it's valid:
        if form.is_valid():
            form.save(commit=True)
            form = stock_news_f()

    # if a GET (or any other method) we'll create a blank form
    else:
        form = stock_news_f()

    return render(request, 'wlist/addstocknews.html', {'form': form})

 

            Related
Django Basic Project Startup Code