The page should display a feedback form and links created in the admin panel. Created models:
from django.db import models
class ContactModel(models.Model):
# feedback form
name = models.CharField(max_length=50)
email = models.EmailField()
website = models.URLField()
message = models.TextField(max_length=5000)
create_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f'{self.name} - {self.email}'
class ContactLink(models.Model):
# links
icon = models.FileField(upload_to='icons/')
name = models.CharField(max_length=200)
def __str__(self):
return self.name
I connected the models to the admin panel:
from django.contrib import admin
from .models import ContactModel, ContactLink
@admin.register(ContactModel)
class ContactModelAdmin(admin.ModelAdmin):
list_display = ['id', 'name', 'email', 'create_at']
list_display_links = ('name',)
admin.site.register(ContactLink)
Everything works correctly, you can create links in the admin panel.
Below views.py
:
from django.shortcuts import render
from django.views import View
from .models import ContactLink
class ContactView(View):
def get(self, request):
contacts = ContactLink.objects.all()
form = ContactForm()
return render(request, 'contact/contact.html', {'contact': contacts, 'form': form})
urls.py
:
from django.urls import path
from . import views
urlpatterns = [
path('contact/', views.ContactView.as_view(), name='contact'),
path('feedback/', views.CreateContact.as_view(), name='feedback'),
]
I go through the for
loop through the code to return links from the admin panel:
#links
<div class="contact__widget">
<ul>
{% for contact in contacts %}
<li>
<img src="{{ contact.icon.url }}">
<span>{{ contact.name }}</span>
</li>
{% endfor %}
</ul>
</div>
#feedback form
<form action="{% url 'feedback' %}" method="post">
{{ form }}
<button type="submit" class="site-btn">Submit</button>
</form>
The Feedback form works fine, but links are not displayed at all on the page... Where could the error be? Screenshot
CodePudding user response:
The view must reference to the correct variable name. Use contact
with an s
in:
return render(request, 'contact/contact.html', {'contacts': contacts, 'form': form})