导入weasyprint的HTML时出现循环导入错误

2024-09-30 22:16:20 发布

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

我试图生成一个pdf文件,我得到下面的错误。你知道吗

raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) django.core.exceptions.ImproperlyConfigured: The included URLconf 'tenderwiz.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

当我在视图中包含这一行时,会发生错误

from weasyprint import HTML

实用程序.py

from io import BytesIO from django.http import HttpResponse from django.template.loader import get_template from django.conf import settings import os from django.template.loader import render_to_string from weasyprint import HTML def render_to_pdf(template_src, context_dict={}): html_template = render_to_string('invoice_render.html', context_dict) pdf_file = HTML(string=html_template).write_pdf() response = HttpResponse(pdf_file, content_type='application/pdf') response['Content-Disposition'] = 'filename="invoice.pdf"' return response

用户帐户/视图.py

from .utils import render_to_pdf from django.shortcuts import render, render_to_response, redirect, get_object_or_404 from django.http import HttpResponseRedirect, Http404 from django.contrib import auth from django.template.context_processors import csrf from packages.models import Packages from .forms import (CustomUserForm, CompanyProfileForm, CompanyProfileEditForm, BankingDetailsForm, PayFast_Form, ) from django.contrib.auth import update_session_auth_hash from .models import CompanyProfile, OurDetails from django.contrib.auth.models import User from django.contrib.auth.forms import PasswordChangeForm, PasswordResetForm from tender_details.models import Category, Keywords, Province from django.core import serializers from django.http import HttpResponse from django.contrib import messages from django.urls import reverse import json from urllib.parse import urlencode, quote_plus import hashlib from django.conf import settings . . .

tenderwiz公司/网址.py

from django.contrib import admin from django.urls import path, include from django.conf.urls import url from tenderwiz.views import privacy_policy_view, termsAndConditions_view from homepage.views import province_view urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^', include('homepage.urls')), url(r'^user_accounts/', include('user_accounts.urls')), url(r'^about_us/', include('about_us.urls')), url(r'^contact_us/', include('contact_us.urls')), url(r'^pricing/', include('pricing.urls')), url(r'^privacy_policy/$', privacy_policy_view, name='privacy_policy'), url(r'^province/(?P<province_pk>\d+)/$', province_view, name='province'), url(r'^termsAndConditions/$', termsAndConditions_view, name='termsAndConditions') ]

回溯

Exception in thread django-main-thread: Traceback (most recent call last): File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 581, in url_patterns iter(patterns) TypeError: 'module' object is not iterable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner self.run() File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run self._target(*self._args, **self._kwargs) File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 54, i n wrapper fn(*args, **kwargs) File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\runserv er.py", line 117, in inner_run self.check(display_num_errors=True) File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 3 90, in check include_deployment_checks=include_deployment_checks, File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 3 77, in _run_checks return checks.run_checks(**kwargs) File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\registry.py", line 7 2, in run_checks new_errors = check(app_configs=app_configs) File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 40, i n check_url_namespaces_unique all_namespaces = _load_all_namespaces(resolver) File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\checks\urls.py", line 57, i n _load_all_namespaces url_patterns = getattr(resolver, 'url_patterns', []) File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\functional.py", line 80, i n __get__ res = instance.__dict__[self.name] = self.func(instance) File "C:\Users\Ayanda\AppData\Local\Programs\Python\Python36\lib\site-packages\django\urls\resolvers.py", line 588, in url_patterns raise ImproperlyConfigured(msg.format(name=self.urlconf_name)) django.core.exceptions.ImproperlyConfigured: The included URLconf 'tenderwiz.urls' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

任何帮助都将不胜感激。你知道吗


Tags: djangoinfrompyimporturlliblocal