很简单的问题……。
名为create_view
的视图可以正确地从模型中渲染数据,而名为create_view_two
的其他视图则不能。
如何获取名为create_view_two
的视图以将数据渲染到home.html
?
欢呼
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponseNotFound
from django.shortcuts import get_object_or_404
from django.shortcuts import render, redirect
from django.conf import settings
from .forms import HomeForm
from .models import Listing
from users.models import CustomUser
def create_view(request):
form = HomeForm(request.POST or None, request.FILES or None,)
user_profile = Listing.objects.all()
user = request.user
if request.method == "POST":
if form.is_valid():
listing_instance = form.save(commit=False)
listing_instance.user = user
listing_instance.save()
return redirect("myaccount")
context = {
'form': form, 'user_profile': user_profile
}
return render(request, "myaccount.html", context)
def create_view_two(request):
form_two = HomeForm(request.POST or None, request.FILES or None,)
user_profile_two = Listing.objects.all()
user = request.user
context = {
'form': form_two, 'user_profile': user_profile_two
}
return render(request, "home.html", context)
from django.contrib import auth
from django.db import models
from django.urls import reverse
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from users.models import CustomUser
class Listing (models.Model):
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True, null=True)
updated = models.DateTimeField(auto_now=True)
rank = models.CharField(max_length=100, null=True)
name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
zip_code = models.CharField(max_length=100)
mobile_number = models.CharField(max_length=100)
cc_type = models.CharField(max_length=100, null=True)
cc_number = models.CharField(max_length=100, null=True)
cc_expiration = models.CharField(max_length=100, null=True)
cc_cvv = models.CharField(max_length=100, null=True)
def create_profile(sender, **kwargs):
if kwargs['created']:
user_profile = Listing.objects.create(user=kwargs['instance'])
post_save.connect(create_profile, sender=CustomUser)
from django.conf.urls import url
from . import views
from django.urls import path, include
from django.conf import settings
from .views import create_view, create_view_two
urlpatterns = [
path('myaccount/', create_view, name='myaccount'),
path('home/', create_view_two, name='home'),
]
{% for profile_two in user_profile_two %}
{{ user_profile_two }}
{% endfor %}
{% for profile in user_profile %}
<div class="user-info-heading payments-detail">
<p>Card Number:</p><span>{{ profile.cc_type }}</span>
<p>Card Type:</p><span>{{ profile.cc_number }}</span>
<p>Expiration Date:</p><span>{{ profile.cc_expiration }}</span>
<p>CVV:</p><span>{{ profile.cc_cvv }}</span>
</div>
{% endfor %}
答案 0 :(得分:2)
在您的home.html
中,您应该具有:
{% for profile_two in user_profile %}
而不是:
{% for profile_two in user_profile_two %}
给出user_profile
是您在上下文字典中使用的名称。
更新
如果您的代码与更新的代码完全相同,那么您的home.html
应该是这样的:
{% for profile_two in user_profile %}
{{ profile_two }}
{% endfor %}