我正在努力为我的项目获得正确的查询。这是一个示例或我的模型:
class OrderItem(models.Model):
order_type = models.CharField(max_length=50, blank=True, null=True)
certification_no = models.CharField(max_length=10, default="43534")
client = models.ForeignKey(Client, blank=True, null=True)
item = models.CharField(max_length=500)
quantity = models.PositiveIntegerField(default=1)
rate = models.DecimalField(decimal_places=2, max_digits=7)
signer = models.CharField(max_length=500)
item_value = models.DecimalField(decimal_places=2, max_digits=7)
certification_type = models.CharField(max_length=200)
packing_list = models.FileField(upload_to='files/%Y-%m-%d', blank=True, null=True)
wholesale = models.BooleanField(default=False)
auth_fee = models.DecimalField(decimal_places=2, max_digits=7, blank=True, null=True)
label_position = models.CharField(max_length=250)
order = models.ForeignKey('Order', blank=True, null=True)
ordered = models.BooleanField(default=False)
created_on = models.DateTimeField(auto_now_add = True)
def __unicode__(self):
return self.item
def total_auth_fee(self):
return self.quantity * self.auth_fee
class Meta:
ordering = ['-created_on']
class Client(models.Model):
user = models.OneToOneField(User)
phone_number = models.PositiveIntegerField(max_length=30,blank=False)
shipping_address = models.CharField(max_length=500, null=True)
zipcode = models.CharField(max_length=30,blank=False)
class Meta:
verbose_name_plural = "Client"
def __unicode__(self):
return self.user.username
如何获取phone_number,(客户端名称和电子邮件地址)是内置的django表单?谢谢
答案 0 :(得分:0)
在您的HTML代码中,您可以轻松访问用户信息。例如
<p>{{ user.username }}</p>
<p>{{ user.email }}</p>
<p>{{ user.first_name }}</p>
<p>{{ user.last_name }}</p>
如果您想以Django格式访问它们,只需在代码中添加from django.contrib.auth.models import User
例如看看这段代码。它是添加新用户的简单表单
<强> views.py 强>
def Signup(request):
if request.method == "POST":
if not request.user.is_authenticated():
form = UserCreationForm(request.POST)
if form.is_valid():
new_username = form.cleaned_data['username']
new_password = form.cleaned_data['password1']
new_email = form.cleaned_data['email']
new_first_name = form.cleaned_data['first_name']
new_last_name = form.cleaned_data['last_name']
#hashed_password = hashlib.md5(new_password).hexdigest()
new_user = User.objects.create_user(
username = new_username,
email = new_email,
first_name = new_first_name,
last_name = new_last_name)
if new_password:
new_user.set_password(new_password)
else:
new_user.set_unusable_password()
new_user.save()
new_user.is_active = True
auth_user = authenticate(username = new_username, password = new_password)
if auth_user is not None:
login(request, auth_user)
return HttpResponseRedirect('../')
else:
form = UserCreationForm()
return render(request, 'Dashboard/signup.html', {'form': form})
<强> forms.py 强>
from django.contrib.auth.forms import UserCreationForm
class SignupForm1(UserCreationForm):
class Meta:
model = User
fields = ('username', 'password', 'email', 'first_name' , 'last_name')
如果您查看forms.py
SignupForm1
继承自User
,那么您可以获得用户信息(字段)