Довольно простой вопрос….
Мое представление с именем create_view
правильно отображает данные из моей модели, а другое представление с именем create_view_two
не выполняет.
Как получить представление с именем create_view_two
для отображения данных в home.html
?
Приветствия
user_profile / views.py
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)
user_profile / модель
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)
user_profile / urls.py
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'),
]
home.html
{% for profile_two in user_profile_two %}
{{ user_profile_two }}
{% endfor %}
myaccount.html
{% 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 %}