Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

model - message sent from contact page dont reflect on the django admin database

am working on contact me page where if a message/feedback is sent, it reflects in django admin database, the page seems to be working fine, however, when the message is sent, it does not show up in admin database

below is my models.py, views.py admin.py as well as part of my html file,

from django.db import models

# Create your models here.
class Contacts(models.Model):
    name = models.CharField(max_length=20 , null=True, editable=False, help_text="Name of sender")
    email = models.EmailField(max_length=50 , null=True, editable=False)
    subject = models.CharField(max_length=100 , null=True, editable=False)
    message = models.TextField(null=True, editable=False)
    date = models.DateTimeField(auto_now_add=True)
    class Meta:
        verbose_name_plural = "Feedback"

    def __str__(self):
        return self.name + "" + self.email

views.py

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from .models import Contacts
#from django.core.mail import send_mail, BadHeaderError
#from django.conf import settings
from django.contrib import messages

# Create your views here.  
def Contacts(request):
    if request.method == 'POST':
       messages.add_message(request, messages.INFO, 'Feedback Submitted.')
       return redirect('Feedback')

    return render(request, 'Contacts/index.html', {})
def Feedback(request):
   return HttpResponse("Thank you for the feedback, a response will be sent to you shortly!")

admin.py

from django.contrib import admin
from .models import Contacts

# Register your models here.
class ContactsAdmin(admin.ModelAdmin):
    list_display = ('name', 'email', 'subject','date',)
    search_fields = ('name', 'email',)
    date_hierarchy = 'date'

admin.site.register(Contacts, ContactsAdmin)

html file

         <form method="post" action="{% url 'Contacts' %}">
        {% csrf_token %}
        <div class="form-group">
          <label class="sr-only" for="fname">First Name *</label>
          <input class="required form-control" id="name" name="name" placeholder="First Name&nbsp;*" type="text" required="required">
        </div>
        <div class="form-group">
          <label class="sr-only" for="lname">Last Name *</label>
          <input class="required form-control" id="message-lname" name="message-lname" placeholder="Last Name&nbsp;*" type="text" required="required">
        </div>
        <div class="form-group">
          <label class="sr-only" for="contactEmail">Email *</label>
          <input type="email" class="required form-control h5-email" id="from-email" name="from-email" placeholder="Email&nbsp;*" type="text" required="required">
        </div>
        <div class="form-group">
          <label class="sr-only" for="contactPhone">Subject *</label>
          <input class="required form-control h5-phone" id="message-subject" name="message-subject" placeholder="Subject&nbsp;*" type="text" required="required">
        </div>
        <div class="form-group">
          <label class="sr-only" for="comment">Type your message here</label>
          <textarea class="required form-control" id="message" name="message" placeholder="Type your message here&nbsp;*" rows="6" required="required"></textarea>
        </div>
        <button class="btn btn-accent" type="submit">Submit</button>  
      </form>
    </div>
question from:https://stackoverflow.com/questions/65844808/message-sent-from-contact-page-dont-reflect-on-the-django-admin-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...