【问题标题】:How can i call views from Html Templates in Django?如何从 Django 中的 Html 模板调用视图?
【发布时间】:2018-05-22 09:54:20
【问题描述】:

当我尝试上传文件并发送一些功能时,我在从views.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')
]

【问题讨论】:

  • 根据您当前的urlpatternsaction 属性应为action = "{% url 'saved' %}"

标签: django


【解决方案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>

【讨论】:

    猜你喜欢
    • 2014-08-18
    • 2010-10-22
    • 2013-07-10
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-17
    相关资源
    最近更新 更多