def create(request): if request.session.has_key('username'): account = Account.objects.get(username = request.session['username']) if account.accounttypeid.accounttypeid == 1: if request.method == 'POST': item = Item( accountid = Account.objects.get(accountid = request.POST['accountid']), lessonid = Lesson.objects.get(lessonid = request.POST['lessonid']), itemname=request.POST['itemid'], createdate= datetime.now(), editdate= datetime.now(), description=request.POST['description'], content=request.POST['content'], order=request.POST['order'], isenable=request.POST['isenable'], note=request.POST['note']) item.save() return redirect('/adminitem/') else: lessons = Lesson.objects.all() accounts = Account.objects.all() subjects = Subject.objects.all() for lesson in lessons: lesson.createdate = lesson.createdate lesson.editdate = lesson.editdate for account in accounts: account.createdate = account.createdate account.editdate = account.editdate for subject in subjects: subject.createdate = subject.createdate subject.editdate = subject.editdate userdetail=UserDetail.objects.get(accountid=account) context = { 'userdetail':userdetail, 'account':account, 'lessons': lessons, 'accounts': accounts, 'subjects': subjects, } return render(request, 'adminitem/item_create.html', context) else: return redirect('homepage:index') else: return redirect('homepage:index')
def Login(request): #User Login Function if request.method == "POST": username = request.POST['username'] password = request.POST['password'] user = auth.authenticate(username= username, password = password) if user is not None : auth.login(request, user) items = Item() items = Item.objects.all() return render(request, "index.html", {'items': items}) else: messages.info(request, "invalid credentials") return redirect('Login') else: return render(request, 'login.html')
import json from homepage.models import Item # Choose csv with item_name and description to fill up items path = './homepage/MOCK_DATA.json' with open(path) as json_file: data = json.load(json_file) for item in data: items = Item(name=item['Item_name'],description=item['Description']) items.save() print("Success")
def index(request): items = Item() items = Item.objects.all() return render(request, 'index.html', {'items': items})