标签: django-errors

Django模板错误:无法解析剩余部分:','来自'uid',?

我正在使用Django forgot_password框架与自定义模板.我在用Django 1.5.我的自定义模板password_reset_email.html如下所示:

{% autoescape off %}
You're receiving this e-mail because you requested a password reset for your user account at {{ site_name }}.

Please go to the following page and choose a new password:
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url django.contrib.auth.views.password_reset_confirm uidb36=uid, token=token %}
{% endblock %}

Your username, in case you've forgotten: {{ user.username }}

Thanks for using our site!

The {{ site_name }} team.

{% endautoescape %} …
Run Code Online (Sandbox Code Playgroud)

django forgot-password django-errors

6
推荐指数
2
解决办法
4743
查看次数

5
推荐指数
1
解决办法
1153
查看次数

django - 检查过滤器是否返回查询集中的任何内容

我是django的新手.我需要检查查询集是否返回任何值,如果没有,则跳转到循环中的下一个项目.我试过尝试..除了ObjectDoesNotExist,这是行不通的.如果过滤器没有找到任何东西,它会返回什么?我该如何检查?

这是现有的代码:

def assign_family_riders(leg):
    remaining_leg_riders = list(leg.riders.all())
    for car in CarAssignment.objects.filter(leg=leg):
        driver_family = car.driver.family
        try:
            riders = leg.riders.all().filter(family=driver_family)
        except ObjectDoesNotExist:
            continue
        for rider in riders:
            car.riders.add(rider)
            remaining_leg_riders.remove(rider)
    return remaining_leg_riders
Run Code Online (Sandbox Code Playgroud)

django django-errors

5
推荐指数
1
解决办法
6620
查看次数

Django的形式错误消息不显示

所以我有自己的看法:

def home_page(request):
    form = UsersForm()
    if request.method == "POST":
        form = UsersForm(request.POST)

        if form.is_valid():
            form.save()
    c = {}
    c.update(csrf(request))
    c.update({'form':form})
    return render_to_response('home_page.html', c)
Run Code Online (Sandbox Code Playgroud)

我的forms.py:

class UsersForm(forms.ModelForm):

    class Meta:
        model = Users
        widgets = {'password':forms.PasswordInput()}

    def __init__(self, *args, **kwargs):
        super( UsersForm, self ).__init__(*args, **kwargs)
        self.fields[ 'first_name' ].widget.attrs[ 'placeholder' ]="First Name"  
        self.fields[ 'last_name' ].widget.attrs[ 'placeholder' ]="Last Name"
        self.fields[ 'password' ].widget.attrs[ 'placeholder' ]="Password"
Run Code Online (Sandbox Code Playgroud)

和我的模板:

<html>
    <body>

    <form method="post" action="">{% csrf_token %}
        {{ form.first_name }} {{form.last_name }} <br>
        {{ form.password }} <br>
        <input …
Run Code Online (Sandbox Code Playgroud)

django django-templates django-models django-forms django-errors

5
推荐指数
2
解决办法
1万
查看次数

ModelChoiceField 上的验证错误

我的 django 表单集中有验证错误。从数据库填充的两个下拉列表没有通过验证,我不明白我的错误是什么。

在此处输入图片说明

型号

class Country(models.Model):
    country_code=models.CharField(max_length=2, primary_key=True)
    country=models.CharField(max_length=20, unique=True)

    def __unicode__(self):
        return u"%s" % self.country

class Status(models.Model):
    verbatim = models.ForeignKey(Verbatim)
    country = models.ForeignKey(Country)
    status = models.CharField(max_length=5, db_index=True)

    def __unicode__(self):
        return u"%s" % self.status

    class Meta:
        unique_together=(("verbatim", "country"), )

class ImportMinAttend(models.Model):
    country=models.CharField(max_length=2, blank=False, null=False)
    verbatim=models.CharField(max_length=250, blank=False, null=False)
    status=models.CharField(max_length=5, blank=True, null=True, default=None)
Run Code Online (Sandbox Code Playgroud)

形式

class MinAttendForm(forms.ModelForm):
    country=forms.ModelChoiceField(queryset=Country.objects.all(), empty_label="Select a country")
    status=forms.ModelChoiceField(queryset=Status.objects.values_list('status', flat = True).distinct(), empty_label="Select a status")
    class Meta:
        model=ImportMinAttend
        #fields used for the validation
        fields = ('country', 'verbatim', 'status') …
Run Code Online (Sandbox Code Playgroud)

forms django validation django-errors

5
推荐指数
1
解决办法
2402
查看次数

Django自定义500错误模板未显示request.user

我正在实现自定义404和500模板,但是404.html模板似乎返回request.user.is_authenticated很好,但是500.html模板无法返回任何内容。我还检查了request.user,它在500页上只是空白。

这很奇怪,因为当我触发500错误时,我收到了预期的错误报告电子邮件,并且显然在请求明细中正确定义了USER。这是我在views.py中使用的代码:

def handler404(request):
    response = render_to_response('404.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 404
    return response


def handler500(request):
    response = render_to_response('500.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 500
    return response
Run Code Online (Sandbox Code Playgroud)

我想知道后台(也许在RequestContext中)是否在处理500与404不同的问题?我应该提到,我也在使用django-guardian,尽管我认为这不会影响任何情况。有任何想法吗?

编辑: 此评论声称“ 500模板将不会呈现request.user,因为它报告了500服务器错误,因此服务器无法提供任何服务。” 有谁知道解决这个问题的方法吗?似乎应该有一个,因为,就像我说的那样,我在错误报告电子邮件中收到的日志显然带有带有用户名的请求对象。

编辑2:现在我想知道它是否与django-allauth有关-我也在使用它。

django django-templates django-errors django-allauth django-1.6

5
推荐指数
1
解决办法
2470
查看次数

Debug=False 中不存在 url 的 Django 500 错误

我在 python 3.6.7 中使用 Django 2.1.3。假设我有这个 URL 路径:

path('admin/', admin.site.urls)
Run Code Online (Sandbox Code Playgroud)

如果我浏览到/ad,而DEBUG = True我看到普通的Django 404错误页面:

在此处输入图片说明

但是,如果我让DEBUG = False服务器向我显示500.htm而不是404.html无效的 URL /ad(不匹配任何 URL 模式)。

404.html是否会显示该引发404错误一个有效的URL。(例如当函数发生不存在的对象查询时get_object_or_404

这是我的/templates目录结构:

/templates
    400.html
    403.html
    404.html
    500.html
    index.html
    ...
Run Code Online (Sandbox Code Playgroud)

那么,如果请求 URL 不匹配任何 URL 模式,我应该如何告诉 Django 在生产中(除了开发之外)显示 404 错误?

笔记:

  • 根据docs,如果我404.html在根模板目录中有一个,则此 404.html 将与默认错误处理程序一起使用。

更新:

我发现这是因为defaults.page_not_found引发了 a Resolver404,这是由以下原因引起的:

传递给 resolve() 的路径没有映射到视图

这正是发生的事情,(/ad不匹配任何视图)

这是我的 …

django django-urls django-errors

5
推荐指数
1
解决办法
1404
查看次数

Django-rest-framework 中的全局异常处理

有没有一种方法可以在不使用 django rest 框架中的 try-except 块的情况下全局处理所有异常。

我想将 django 抛出的 html 错误页面转换为自定义的 json 对象响应。

我在我的应用程序中创建了一个 exception.py 文件

def custom_exception_handler(exc, context=None):
    response = exception_handler(exc)


    if isinstance(exc, HttpResponseServerError):  
        custom_response_data = { 
            'detail': 'Internal Server Error' # custom exception message
        }
        response.data = custom_response_data

    return response
Run Code Online (Sandbox Code Playgroud)

我已经在 settings.py 中配置了这个。

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'EXCEPTION_HANDLER':'my_project.my_app.exceptions.custom_exception_handler'}
Run Code Online (Sandbox Code Playgroud)

django exception django-errors django-rest-framework django-rest-viewsets

5
推荐指数
2
解决办法
5951
查看次数

Django 在其他 IP 地址上的内部链接已损坏

为什么我得到“ Broken INTERNAL link on xyz”,其中 xyz 不是托管网站的服务器的 IP 地址?

更具体地说:我的网站地址是“ myExample.com”,还有另一个 IP 地址,我们称之为“ anotherExample”。我收到“另一个示例上的内部链接损坏”。引荐来源网址是'http://anotherExample/somePHPFile.php',请求的 URL 当然是'/somePHPFile.php'

apache django django-errors

4
推荐指数
1
解决办法
1039
查看次数

将请求数据包含在 Django Rest Framework 自定义异常处理程序响应数据中

使用的技术:

http://www.django-rest-framework.org

例外: http: //www.django-rest-framework.org/api-guide/exceptions/

在自定义Exceptions.py文件中包含rest_framework默认示例:

from rest_framework.views import exception_handler

import sys

def custom_exception_handler(exc, context=None):

    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc)

    # Now add the HTTP status code to the response and rename detail to error
    if response is not None:
        response.data['status_code'] = response.status_code
        response.data['request'] = request
        response.data['error'] = response.data.get('detail')
        del response.data['detail']

    return response
Run Code Online (Sandbox Code Playgroud)

这会发送基本错误信息,如“Http404”等,但不会发送请求数据,如 IP 地址等。

将我的请求添加到响应中的最佳方式?提前致谢。

更新(并解决):

因此,我最初尝试使用 DjangoRestFramework 2.4.x 解决此问题,但该版本没有自定义异常处理程序的请求或上下文数据选项。升级到 3.1.3 可以轻松地将数据添加到响应中。新代码现在如下所示(使用版本 3.1.3):

def custom_exception_handler(exc, …
Run Code Online (Sandbox Code Playgroud)

python django error-handling django-errors django-rest-framework

4
推荐指数
1
解决办法
4028
查看次数