如何在Django中调用Html模板中的视图?

时间:2018-05-22 09:54:21

标签: django

因为我正在尝试上传文件并发送一些功能我从view.py到html模板调用视图功能时遇到错误我不知道我做错了什么可以有人帮我...谢谢你提前:) 这是我的views.py: 我的应用程序名称是secondone,我的视图函数名称是SavedProfile。

来自django.shortcuts导入渲染

from secondone.forms import ProfileForm
from secondone.models import Profile

def SaveProfile(request):
   saved = False

   if request.method == "POST":
      #Get the posted form
      MyProfileForm = ProfileForm(request.POST, request.FILES)

      if MyProfileForm.is_valid():
         profile = Profile()
         profile.name = MyProfileForm.cleaned_data["name"]
         profile.picture = MyProfileForm.cleaned_data["picture"]
         profile.save()
         saved = True
   else:
      MyProfileForm = ProfileForm()

   return render(request, 'last.html', locals())

这是我的模板:

<html>
    <body>
        <form name="form" enctype="multipart/form-data"
        action = "{% url 'views.SaveProfile'  %}" method = "POST" >{% csrf_token %}
         <div style="max-width: 470px">
             <center>
                 <input type="text" style="margin-left:20%;"
                 placeholder="Name" name="name">

             </center>
         </div>
         <br>

         <div style = "max-width:470px;">
            <center> 

               <button style = "border:0px;background-color:#4285F4; margin-top:8%; 
                  height:35px; width:80%; margin-left:19%;" type = "submit" value = "Login" >
                  <strong>Login</strong>
               </button>

            </center>
         </div>
    </body>
</html>

这是我的url.py:

from django.contrib import admin


from myapp import views
from secondone import views


from django.views.generic import TemplateView
from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [

    url(r'^login/$', auth_views.login, name='login'),
    url(r'^logout/$', auth_views.logout, name='logout'),
    url(r'^oauth/', include('social_django.urls', namespace='social')),  # <--
    url(r'^admin/', admin.site.urls),
    url(r'^accounts/',include('allauth.urls')),
    url(r'^profile/',TemplateView.as_view(template_name = 'registeration.html')), 
    url(r'^saved/', views.SaveProfile, name = 'saved')
]

1 个答案:

答案 0 :(得分:1)

我尝试了一些其他技术,我得到了我的输出..

<html>
    <body>
        <form name="form" enctype="multipart/form-data"
        action = "/SaveProfile/" method = "POST" >{% csrf_token %}
         <div style="max-width: 470px">
             <center>
                 <input type="text" style="margin-left:20%;"
                 placeholder="Name" name="name">

             </center>
         </div>
         <br>

         <div style = "max-width:470px;">
            <center> 

               <button style = "border:0px;background-color:#4285F4; margin-top:8%; 
                  height:35px; width:80%; margin-left:19%;" type = "submit" value = "Login" >
                  <strong>Login</strong>
               </button>

            </center>
         </div>
    </body>
</html>