first commit
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
from django.contrib import admin
|
||||
from .models import ContactMessage
|
||||
|
||||
|
||||
@admin.register(ContactMessage)
|
||||
class ContactMessageAdmin(admin.ModelAdmin):
|
||||
list_display = ('name', 'company', 'phone', 'email', 'subject', 'is_read', 'created_at')
|
||||
list_filter = ('is_read', 'created_at')
|
||||
list_editable = ('is_read',)
|
||||
search_fields = ('name', 'company', 'phone', 'email', 'subject', 'message')
|
||||
date_hierarchy = 'created_at'
|
||||
readonly_fields = ('name', 'company', 'phone', 'email', 'subject', 'message', 'ip_address', 'created_at')
|
||||
fieldsets = (
|
||||
('留言信息', {
|
||||
'fields': ('name', 'company', 'phone', 'email', 'subject', 'message')
|
||||
}),
|
||||
('系统信息', {
|
||||
'fields': ('ip_address', 'created_at', 'is_read')
|
||||
}),
|
||||
)
|
||||
|
||||
def has_add_permission(self, request):
|
||||
return False
|
||||
@@ -0,0 +1,7 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ContactConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.contact'
|
||||
verbose_name = '留言管理'
|
||||
@@ -0,0 +1,16 @@
|
||||
from django import forms
|
||||
from .models import ContactMessage
|
||||
|
||||
|
||||
class ContactForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = ContactMessage
|
||||
fields = ['name', 'company', 'phone', 'email', 'subject', 'message']
|
||||
widgets = {
|
||||
'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '请输入姓名'}),
|
||||
'company': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '请输入公司名称'}),
|
||||
'phone': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '请输入联系电话'}),
|
||||
'email': forms.EmailInput(attrs={'class': 'form-control', 'placeholder': '请输入邮箱'}),
|
||||
'subject': forms.TextInput(attrs={'class': 'form-control', 'placeholder': '请输入主题'}),
|
||||
'message': forms.Textarea(attrs={'class': 'form-control', 'placeholder': '请输入留言内容', 'rows': 5}),
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
# Generated by Django 5.0.14 on 2026-07-31 13:52
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='ContactMessage',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=50, verbose_name='姓名')),
|
||||
('company', models.CharField(blank=True, default='', max_length=100, verbose_name='公司名称')),
|
||||
('phone', models.CharField(max_length=20, verbose_name='电话')),
|
||||
('email', models.EmailField(max_length=254, verbose_name='邮箱')),
|
||||
('subject', models.CharField(max_length=100, verbose_name='主题')),
|
||||
('message', models.TextField(verbose_name='留言内容')),
|
||||
('ip_address', models.GenericIPAddressField(blank=True, null=True, verbose_name='IP地址')),
|
||||
('is_read', models.BooleanField(default=False, verbose_name='已读')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='提交时间')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': '在线留言',
|
||||
'verbose_name_plural': '在线留言',
|
||||
'ordering': ['-created_at'],
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,21 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class ContactMessage(models.Model):
|
||||
name = models.CharField('姓名', max_length=50)
|
||||
company = models.CharField('公司名称', max_length=100, blank=True, default='')
|
||||
phone = models.CharField('电话', max_length=20)
|
||||
email = models.EmailField('邮箱')
|
||||
subject = models.CharField('主题', max_length=100)
|
||||
message = models.TextField('留言内容')
|
||||
ip_address = models.GenericIPAddressField('IP地址', blank=True, null=True)
|
||||
is_read = models.BooleanField('已读', default=False)
|
||||
created_at = models.DateTimeField('提交时间', auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ['-created_at']
|
||||
verbose_name = '在线留言'
|
||||
verbose_name_plural = verbose_name
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.name} - {self.subject}'
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.contact, name='contact'),
|
||||
]
|
||||
@@ -0,0 +1,21 @@
|
||||
from django.shortcuts import render, redirect
|
||||
from django.contrib import messages
|
||||
from .forms import ContactForm
|
||||
|
||||
|
||||
def contact(request):
|
||||
breadcrumb_items = [{'name': '联系我们', 'url': ''}]
|
||||
if request.method == 'POST':
|
||||
form = ContactForm(request.POST)
|
||||
if form.is_valid():
|
||||
msg = form.save(commit=False)
|
||||
msg.ip_address = request.META.get('REMOTE_ADDR')
|
||||
msg.save()
|
||||
messages.success(request, '留言提交成功,我们会尽快与您联系!')
|
||||
return redirect('contact')
|
||||
else:
|
||||
form = ContactForm()
|
||||
return render(request, 'contact/index.html', {
|
||||
'form': form,
|
||||
'breadcrumb_items': breadcrumb_items,
|
||||
})
|
||||
Reference in New Issue
Block a user