Sruthipriyanga
Sruthipriyanga

Reputation: 468

How can i call views from Html Templates in Django?

As i'm trying to upload file and send with some functionality i'm getting error while calling view function from views.py to html templates i don't know what i did wrongly Can anyone help me..Thanks in Advance :) here is my views.py: my application name is secondone and my view function name is SavedProfile.

from django.shortcuts import render

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

Here is my Template:

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

Here is my 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')
]

Upvotes: 0

Views: 116

Answers (1)

Sruthipriyanga
Sruthipriyanga

Reputation: 468

i tried with some other techniques and i got my output..

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

Upvotes: 1

Related Questions