代码之家  ›  专栏  ›  技术社区  ›  Sruthipriyanga

如何从Django中的HTML模板调用视图?

  •  0
  • Sruthipriyanga  · 技术社区  · 7 年前

    当我试图上传文件并发送一些功能时,我在从views.py调用view函数到html模板时出错,我不知道我做错了什么,有人能帮我吗?…提前谢谢:) 这是我的观点.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 回复  |  直到 7 年前
        1
  •  1
  •   Sruthipriyanga    7 年前

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

    <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>