would like to press save button then call post_detail and append to csv,
it return error
<button type="submit" class="save btn btn-default" onClick="window.location.href='{% url 'reg/{{post.pk}}' %}'">Save</button>
Reverse for 'reg/{{post.pk}}/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
urls.py
from django.conf.urls import include, url
from . import views
urlpatterns = [
url(r'^reg/$', views.post_new, name='post_new'),
url(r'^reg/(?P<pk>\d+)/$', views.post_detail, name='post_detail'),
]
views.py
from .forms import PostForm
from django.shortcuts import render
from django.template.loader import get_template
def post_new(request):
form = PostForm()
return render(request, 'registration.html', {'form': form})
def post_detail(request, pk):
post = get_object_or_404(Post, pk=pk)
if request.method == "POST":
form = PostForm(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.ProjectName = request.ProjectName
post.UserName = request.UserName
post.Company = request.Company
post.Contact = request.Contact
post.InitialPassword = request.InitialPassword
post.UserType = request.UserType
post.BusinessType = request.BusinessType
post.published_date = timezone.now()
post.save()
return redirect('registration.html', pk=post.pk)
else:
form = PostForm(instance=post)
return render(request, 'registration.html', {'form': form})
models.py
from django.db import models
from django.utils import timezone
from django.apps import AppConfig
import csv
import os.path
USERTYPE = (
('Technology Partner', 'Technology Partner'),
('Others', 'Others'),
)
BUSINESSTYPE = (
('Building', 'Building'),
('Data Analysis', 'Data Analysis'),
)
class MyAppConfig(AppConfig):
name = 'src.my_app_label'
def ready(self):
post_migrate.connect(do_stuff, sender=self)
class Post(models.Model):
author = models.ForeignKey('auth.User')
Email = models.CharField(max_length=70)
ProjectName = models.CharField(max_length=70)
UserName = models.CharField(max_length=70)
Company = models.CharField(max_length=70)
Contact = models.CharField(max_length=70)
InitialPassword = models.CharField(max_length=70)
UserType = models.CharField(max_length=30, choices=USERTYPE)
BusinessType = models.CharField(max_length=30, choices=BUSINESSTYPE)
#UserType = models.ChoiceField(choices=USERTYPE, required=True )
#BusinessType = models.ChoiceField(choices=BUSINESSTYPE, required=True )
#ProjectName = models.TextField()
created_date = models.DateTimeField(default=timezone.now)
published_date = models.DateTimeField(blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
isexist = os.path.isfile('newusers.csv')
with open('/home/martin/Downloads/site1/site1/reg/newusers.csv', 'a') as csvfile:
fieldnames = ['name','email address','project','initial password','userType','contact','businessType','company']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if isexist == false:
writer.writeheader()
writer.writerow({'name': UserName, 'email address': Email, 'project': ProjectName, 'initial password': InitialPassword,'userType': UserType, 'contact': Contact, 'businessType': BusinessType, 'company': Company,})
self.save()
def __str__(self):
return self.title
forms.py
from django import forms
from .models import Post
USERTYPE = (
('Technology Partner', 'Technology Partner'),
('Others', 'Others'),
)
BUSINESSTYPE = (
('Building', 'Building'),
('Data Analysis', 'Data Analysis'),
)
class PostForm(forms.ModelForm):
#if form.is_valid():
#post = form.save(commit=False)
#post.author = request.user
#post.published_date = timezone.now()
#post.save()m
class Meta:
model = Post
fields = ('Email', 'ProjectName', 'UserName', 'Company', 'Contact', 'InitialPassword','UserType','BusinessType')
#fields = ('title', 'text',)
UserType = forms.ChoiceField(choices=USERTYPE, required=True )
BusinessType = forms.ChoiceField(choices=BUSINESSTYPE, required=True )
registration.html
{% block content %}
<h1>New user registration</h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-default" >Save</button>
</form>
{% endblock %}
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8a7be0c7-1ec3-4a65-9747-d974c25f73b1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment