contact form join()参数上的Django错误必须是str、bytes或os.PathLike对象,

2024-10-05 11:04:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为我的学校项目创建一个联系表单,但是遇到了这个错误,我不知道这意味着什么

我根据另一个工作正常的表单创建此表单

我尝试过谷歌搜索,但弄不清楚问题出在哪里

有人能帮我照一下吗

错误:


Traceback (most recent call last):
  File "c:\users\01\appdata\local\programs\python\python39\lib\ntpath.py", line 91, in join
    for p in map(os.fspath, paths):

During handling of the above exception (expected str, bytes or os.PathLike object, not dict), another exception occurred:
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\views\generic\base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\views\generic\base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\Project\bsp\store\contact\views.py", line 11, in get
    return render("contact.html", context)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\shortcuts.py", line 19, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\template\loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\template\loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\template\backends\django.py", line 34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\template\loaders\base.py", line 18, in get_template
    for origin in self.get_template_sources(template_name):
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\template\loaders\filesystem.py", line 36, in get_template_sources
    name = safe_join(template_dir, template_name)
  File "C:\Users\01\Desktop\TUD Y2\Sem4\bsp\store\env\lib\site-packages\django\utils\_os.py", line 17, in safe_join
    final_path = abspath(join(base, *paths))
  File "c:\users\01\appdata\local\programs\python\python39\lib\ntpath.py", line 117, in join
    genericpath._check_arg_types('join', path, *paths)
  File "c:\users\01\appdata\local\programs\python\python39\lib\genericpath.py", line 152, in _check_arg_types
    raise TypeError(f'{funcname}() argument must be str, bytes, or '

管理员

class ContactAdmin(admin.ModelAdmin):
    list_display = ['firstname', 'lastname', 'email', 'subject']
    list_per_page = 20

admin.site.register(Contact, ContactAdmin)

forms.py

class ContactForm(forms.Form):
    firstname=forms.CharField()
    lastname=forms.CharField()
    email = forms.EmailField()
    subject = forms.Textarea()

models.py

class Contact(models.Model):
    firstname=models.CharField(max_length=200)
    lastname=models.CharField(max_length=200)
    email=models.EmailField(null=False)
    subject=models.TextField()

    def __str__(self):
        return f'{self.lastname}, {self.firstname}, {self.email}'

url.py

urlpatterns = [
    path('', ContactView.as_view(), name='contact'),
]

views.py

class ContactView(View):
    def get(self, *args, **kwargs):
        form = ContactForm()
        context = {'form': form}
        return render("contact.html", context)
        
    def post(self, *args, **kwargs):
        form = ContactForm(self.request.POST) 
        if form.is_valid():
            firstname = form.cleaned_data.get('firstname')
            lastname = form.cleaned_data.get('lastname')
            email = form.cleaned_data.get('email')
            subject= form.cleaned_data.get('subject')
            contact = Contact()
            contact.firstname = firstname
            contact.lastname = lastname 
            contact.email = email
            contact.subject = subject
            contact.save()
            messages.info(self.request, "Your message has been received.")
            return render('thanks_contact.html')

项目URL

urlpatterns = [
    path('admin/', admin.site.urls),
    path('accounts/', include('accounts.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('',include('shop.urls')),
    path('search/',include('search_app.urls')),
    path('cart/', include('cart.urls')),
    path('contact/', include('contact.urls')),
    path('order/',include('order.urls')),
    path('blog/', include('blog.urls')),
    path('vouchers/',include('vouchers.urls',namespace='vouchers'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

contact.html

{% extends "base.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block title %}
    Contact 
{% endblock %}
{% block content %}
    <div class = "container">
    <h2>Contact</h2>
    <form method="POST">
    {% csrf_token %}
    {{ form|crispy }}
    <button type='submit' class="btn btn-primary my-3">Submit</button> 
    </form>
    </div>
        
{% endblock content%}

Tags: djangoinpygetliblinesitetemplate
1条回答
网友
1楼 · 发布于 2024-10-05 11:04:31

以下是您需要添加的更改

在URL中:

    from django.urls import path
    from .views import ContactView


    urlpatterns = [
        path('', ContactView.as_view(), name='contact'), #add as_view()
    ]

在意见中:

    from django.shortcuts import render
    from .forms import ContactForm
    from django.views import View
    from .models import Contact
    from django.contrib import messages



    class ContactView(View):
        def get(self,request, *args, **kwargs): #add request
            form = ContactForm()
            context = {'form': form}
            return render(request,"contact.html", context) #add request 
        pass
        def post(self,request, *args, **kwargs): #add request
            form = ContactForm(self.request.POST) 
            if form.is_valid():
                firstname = form.cleaned_data.get('firstname')
                lastname = form.cleaned_data.get('lastname')
                email = form.cleaned_data.get('email')
                subject= form.cleaned_data.get('subject')
                contact = Contact()
                contact.firstname = firstname
                contact.lastname = lastname 
                contact.email = email
                contact.subject = subject
                contact.save()
                messages.info(self.request, "Your message has been received.")
                context = {'form':form} #add context
                return render(request,'thanks_contact.html',context) #add request and context

形式如下:

    from django import forms

    class ContactForm(forms.Form):
        firstname=forms.CharField()
        lastname=forms.CharField()
        email = forms.EmailField()
        subject = forms.CharField(widget=forms.Textarea) # pass textarea as widget

相关问题 更多 >

    热门问题