2017-08-02 1 views
0

importieren Ich will meine Swift App & Server Python Django verbinden Bild in Senden (Ich möchte Bilder von Swift App Server senden) Wenn ich es zu tun versucht, ich habe einen FehlerImport: nicht Name csrf_exemp

<div id="info"> 
    <h2>Help</h2> 

    <p>Reason given for failure:</p> 
    <pre> 
    CSRF cookie not set. 
    </pre> 


    <p>In general, this can occur when there is a genuine Cross Site Request Forgery, or when 
    <a 
    href="https://docs.djangoproject.com/en/1.10/ref/csrf/">Django's 
    CSRF mechanism</a> has not been used correctly. For POST forms, you need to 
    ensure:</p> 

    <ul> 
    <li>Your browser is accepting cookies.</li> 

    <li>The view function passes a <code>request</code> to the template's <a 
    href="https://docs.djangoproject.com/en/dev/topics/templates/#django.template.backends.base.Template.render"><code>render</code></a> 
    method.</li> 

    <li>In the template, there is a <code>{% csrf_token 
    %}</code> template tag inside each POST form that 
    targets an internal URL.</li> 

    <li>If you are not using <code>CsrfViewMiddleware</code>, then you must use 
    <code>csrf_protect</code> on any views that use the <code>csrf_token</code> 
    template tag, as well as those that accept the POST data.</li> 

    <li>The form has a valid CSRF token. After logging in in another browser 
    tab or hitting the back button after a login, you may need to reload the 
    page with the form, because the token is rotated after a login.</li> 
    </ul> 

    <p>You're seeing the help section of this page because you have <code>DEBUG = 
    True</code> in your Django settings file. Change that to <code>False</code>, 
    and only the initial error message will be displayed. </p> 

    <p>You can customize this page using the CSRF_FAILURE_VIEW setting.</p> 
</div> 

</body> 
</html> 

Also, ich denke, vielleicht Hinzufügen von CSRF Dekoratoren zu Django Server ist gut. ich es zu meiner Codes wie

from django.contrib.auth.forms import AuthenticationForm 
from django.contrib.auth.decorators import login_required 
from django.http import HttpResponse 
from django.shortcuts import render, redirect 
from django.views.decorators.http import require_POST 
from .forms import RegisterForm 
from django.contrib.auth import authenticate, login 
from .models import Post 
from .forms import UserImageForm 
from .models import ImageAndUser 
from django.views.decorators.csrf import csrf_exemp 
@csrf_exempt 
def upload_save(request): 

    photo_id = request.POST.get("p_id", "") 

    if (photo_id): 
     photo_obj = Post.objects.get(id=photo_id) 
    else: 
     photo_obj = Post() 

    files = request.FILES.getlist("files[]") 


    photo_obj.image = files[0] 
    # photo_obj.image2 = files[1] 
    # photo_obj.image3 = files[2] 

    photo_obj.save() 
    # return render(request, "registration/accounts/photo.html") 

    photos = Post.objects.all() 
    context = { 
     'photos': photos, 
    } 
    return render(request, 'registration/accounts/photo.html', context) 

Aber, wenn ich meinen Server laufen, Fehler von django.views.decorators.csrf Import csrf_exemp Import passiert: kann nicht Namen csrf_exemp importieren. Traceback sagt

Unhandled exception in thread started by <function wrapper at 0x103947b90> 
Traceback (most recent call last): 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper 
    fn(*args, **kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run 
    self.check(display_num_errors=True) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 374, in check 
    include_deployment_checks=include_deployment_checks, 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 361, in _run_checks 
    return checks.run_checks(**kwargs) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks 
    new_errors = check(app_configs=app_configs) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config 
    return check_resolver(resolver) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver 
    for pattern in resolver.url_patterns: 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 313, in url_patterns 
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/urls/resolvers.py", line 306, in urlconf_module 
    return import_module(self.urlconf_name) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/Users/XXX/Desktop/KenshinServer/KenshinServer/urls.py", line 24, in <module> 
    url(r'^accounts/', include('accounts.urls')), 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include 
    urlconf_module = import_module(urlconf_module) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/Users/XXX/Desktop/KenshinServer/accounts/urls.py", line 2, in <module> 
    from . import views 
    File "/Users/XXX/Desktop/KenshinServer/accounts/views.py", line 11, in <module> 
    from django.views.decorators.csrf import csrf_exemp 
ImportError: cannot import name csrf_exemp 

Warum dieser Fehler passieren? Ich denke, das Hinzufügen nur from django.views.decorators.csrf import csrf_exemp & @csrf_exempt genug, aber es ist nicht in Ordnung? (Soll ich etwas anderes hinzufügen?) Wie kann ich dieses Problem beheben?

+2

Sie haben einen Tippfehler, Sie sind ein 't' in dem Import fehlen line - csrf_exempt –

+2

es ist 'django.views.decorators.csrf importieren csrf_exempt' Sie haben einen Tippfehler gemacht –

Antwort

2

Es gibt Tippfehler Fehler in Ihrem Code ist es csrf_exempt nicht csrf_exemp

0

Änderung

from django.views.decorators.csrf import csrf_exemp 

zu

from django.views.decorators.csrf import csrf_exempt