python - GET method has taken by Django -
i beginner in django , creating django forms getting input , processing. created app called 'artists' , have coded files follows. error in picture urls.py in server folder
"""mysite url configuration `urlpatterns` list routes urls views. more information please see: https://docs.djangoproject.com/en/1.11/topics/http/urls/ examples: function views 1. add import: my_app import views 2. add url urlpatterns: url(r'^$', views.home, name='home') class-based views 1. add import: other_app.views import home 2. add url urlpatterns: url(r'^$', home.as_view(), name='home') including urlconf 1. import include() function: django.conf.urls import url, include 2. add url urlpatterns: url(r'^blog/', include('blog.urls')) """ django.conf.urls import url, include django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^artists/', include('artists.urls')), ]
these files in 'artists' app -> urls.py <-
from django.conf.urls import url . import views urlpatterns = [ url(r'^artists/', views.artistcreate, name="artistcreate")]
-> views.py <-
from django.shortcuts import render, render_to_response django.template import requestcontext datetime import datetime artists.models import * def artistcreate(request): if request.method=="get": form=artistform() return render(request,'artists/create.html',{'form':form}) elif request.method=="post": form=artistform(request.post) form.save() return httpresponseredirect('/artists')
-> models.py <-
from django.db import models django.forms import modelform class artist(models.model): name=models.charfield(max_length=50) year_formed=models.positiveintegerfield() class artistform(modelform): class meta: model=artist fields=['name','year_formed']
-> artists/create.html <- html file in templates folder
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"/> <title>test</title> </head> <body> <form method="post"> {% csrf_token %} {{ form }} <button type="submit">save</button> </form> </body> </html>
i need create django form receives input , can able process.thanks in advance
you need call form.is_valid()
before saving form. when is_valid()
method called, validations on form run , boolean returned whether form
valid or not.
you may need changes in view,
def artistcreate(request): if request.method=="post": form = artistform(request.post) if form.is_valid(): form.save() return httpresponseredirect('/artists') else: form = artistform() return render(request,'artists/create.html',{'form':form})
Comments
Post a Comment