我的model.py,views.py forms.py,html模板已完成,但我无法从数据库检索数据
我试图运行我的views python文件,但它无法运行,并且显示了相对导入错误。我的代码中是否存在错误或其他问题。请说明一下,因为我在这方面浪费了一整天,结果为0
我的视图中出现了错误。请注意
文件“views.py”,第5行,在
从。表格导入注册1
ImportError:尝试在没有已知父包的情况下进行相对导入
views.py
从django.shortcuts导入渲染
#from django.http import RequestContext
#from django.http import HttpResponseRedirect
from .forms import Registration1
from .models import Category
#print('__file__={0:<35} | __name__={1:<20} | __package__={2:
#<20}'.format(__file__,__name__,str(__package__)))
#count = 5
# Create your views here.
def home(request):
#print(context_dict.values())
if request.method == 'POST':
stud1 = ' '
cm =Registration1(request.POST)
if cm.is_valid():
cm1 = cm.cleaned_data['category']
sm1 = cm.cleaned_data['subcateg']
pm1 = cm.cleaned_data['product']
reg2 = Category(category = cm1,subcateg = sm1,product = pm1)
reg2.save()
cm = Registration1()
else :
cm = Registration1()
stud1 = Category.objects.all()
print(stud1)
return render(request,'registration.html', {'forms': cm ,'Register' :
stud1}
)
Models.py
from django.conf import settings
from django.db import models
class Category(models.Model):
category = models.CharField(max_length=50,null =True)
subcateg = models.CharField(max_length=50,null = True)
product = models.CharField(max_length=50,null = True)
Forms.py
from django import forms
from .models import Category
class Registration1(forms.ModelForm) :
class Meta :
model = Category
fields = ['product','subcateg','category']
widgets = {
'product' : forms.TextInput(attrs={'class':'form-control'}),
'subcateg' : forms.TextInput(attrs={'class':'form-control'}),
'category' : forms.TextInput(attrs={'class':'form-control'})
,
}
Registration.html
<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel = "stylesheet" href ="{% static 'project/css/bootstrap.css '
%}">
</head>
<body>
<div class = "container mt-5">
<div class = "row">
<div class = "col-sm-0">
<h4 class = "text-centre alert alert-info ">Add Product </h4>
<form action = '' method = "POST">
{% csrf_token %}
{{forms.as_p}}
<br>
<input type = "submit" class = "btn btn-success"
value="add">
</form>
</div>
<div class="col-sm-8">
<h4 class = "text-centre alert alert-info">DataBase Table</h4>
{% if Register %}
<table class="table table-striped table-dark">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">PRODUCT</th>
<th scope="col">SUBCATEGORY</th>
<th scope="col">CATEGORY</th>
</tr>
</thead>
<tbody>
{% for st in Register %}
<tr>
<th scope ="row">{{st.id}}</th>
<td>{{st.product}}</td>
<td>{{st.subcateg}}</td>
<td>{{st.category}}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
</div>
</div>
<script src = "{% static 'project/css/jQuery.js' %}"></script>
<script src = "{% static 'project/css/popper.js'%}"></script>
<script src = "{% static 'project/css/bootstrap.js' %}"></script>
</body>
</html>
所以我想你需要声明一个像here这样的外键。 应该是这样的
我不知道模板中是否有错误,因为你没有发布它们
相关问题 更多 >
编程相关推荐