From submitted twice
I wanted to upload any file. But when I submit the upload then same post is uploaded twice but in one of it there is no file but just the content and in next there is file.
class FileCreateView(PassRequestMixin, SuccessMessageMixin,
CreateView):
template_name = 'file/upload-file.html'
form_class = FileForm
success_message = 'File was uploaded successfully'
success_url = reverse_lazy('home')
def post(self, *args, **kwargs):
"""
Handle POST requests: instantiate a form instance with the passed
POST variables and then check if it's valid.
"""
# form = self.get_form([self.request.FILES])
form = self.form_class(self.request.POST, self.request.FILES)
if self.request.method == 'POST':
if form.is_valid():
file = form.save(commit=False)
file.upload = form.cleaned_data['upload']
file.author = User.objects.get(pk=self.request.user.pk)
file.save()
return self.form_valid(form)
else:
return self.form_invalid(form)
home.html
<div class="container mt-3">
<div class="row">
<div class="col-12 mb-3">
<button class="upload-file btn btn-primary" type="button" name="button">
<span class="fa fa-plus mr-2"></span>Upload Form</button>
</div>
</div>
{% block extrascripts %}
<script type="text/javascript">
$(function () {
$(".upload-file").modalForm({formURL: "{% url 'file-upload' %}"});
});
</script>
{% endblock extrascripts %}
urls.py
path('upload/file/', FileCreateView.as_view(), name="file-upload"),
model.py
class File(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
author = models.ForeignKey(User, on_delete=models.CASCADE)
visible_to_home = models.ManyToManyField(Home, blank=True) # when none visible to all home
visible_to_company = models.ManyToManyField(Company, blank=True) # when none visible to all company
# To determine visibility, check if vtc is none or include company of user and if true, check same for home
created_date = models.DateTimeField(auto_now=True)
published = models.BooleanField(default=True)
upload = models.FileField(blank=True, null=True, upload_to=update_filename)
title = models.CharField(max_length=225, blank=True, null=True)
description = models.TextField(blank=True, null=True)
In one of the uploaded file there is no upload
field of the model and the next of the twice there is everything.
django django-forms django-views
add a comment |
I wanted to upload any file. But when I submit the upload then same post is uploaded twice but in one of it there is no file but just the content and in next there is file.
class FileCreateView(PassRequestMixin, SuccessMessageMixin,
CreateView):
template_name = 'file/upload-file.html'
form_class = FileForm
success_message = 'File was uploaded successfully'
success_url = reverse_lazy('home')
def post(self, *args, **kwargs):
"""
Handle POST requests: instantiate a form instance with the passed
POST variables and then check if it's valid.
"""
# form = self.get_form([self.request.FILES])
form = self.form_class(self.request.POST, self.request.FILES)
if self.request.method == 'POST':
if form.is_valid():
file = form.save(commit=False)
file.upload = form.cleaned_data['upload']
file.author = User.objects.get(pk=self.request.user.pk)
file.save()
return self.form_valid(form)
else:
return self.form_invalid(form)
home.html
<div class="container mt-3">
<div class="row">
<div class="col-12 mb-3">
<button class="upload-file btn btn-primary" type="button" name="button">
<span class="fa fa-plus mr-2"></span>Upload Form</button>
</div>
</div>
{% block extrascripts %}
<script type="text/javascript">
$(function () {
$(".upload-file").modalForm({formURL: "{% url 'file-upload' %}"});
});
</script>
{% endblock extrascripts %}
urls.py
path('upload/file/', FileCreateView.as_view(), name="file-upload"),
model.py
class File(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
author = models.ForeignKey(User, on_delete=models.CASCADE)
visible_to_home = models.ManyToManyField(Home, blank=True) # when none visible to all home
visible_to_company = models.ManyToManyField(Company, blank=True) # when none visible to all company
# To determine visibility, check if vtc is none or include company of user and if true, check same for home
created_date = models.DateTimeField(auto_now=True)
published = models.BooleanField(default=True)
upload = models.FileField(blank=True, null=True, upload_to=update_filename)
title = models.CharField(max_length=225, blank=True, null=True)
description = models.TextField(blank=True, null=True)
In one of the uploaded file there is no upload
field of the model and the next of the twice there is everything.
django django-forms django-views
add a comment |
I wanted to upload any file. But when I submit the upload then same post is uploaded twice but in one of it there is no file but just the content and in next there is file.
class FileCreateView(PassRequestMixin, SuccessMessageMixin,
CreateView):
template_name = 'file/upload-file.html'
form_class = FileForm
success_message = 'File was uploaded successfully'
success_url = reverse_lazy('home')
def post(self, *args, **kwargs):
"""
Handle POST requests: instantiate a form instance with the passed
POST variables and then check if it's valid.
"""
# form = self.get_form([self.request.FILES])
form = self.form_class(self.request.POST, self.request.FILES)
if self.request.method == 'POST':
if form.is_valid():
file = form.save(commit=False)
file.upload = form.cleaned_data['upload']
file.author = User.objects.get(pk=self.request.user.pk)
file.save()
return self.form_valid(form)
else:
return self.form_invalid(form)
home.html
<div class="container mt-3">
<div class="row">
<div class="col-12 mb-3">
<button class="upload-file btn btn-primary" type="button" name="button">
<span class="fa fa-plus mr-2"></span>Upload Form</button>
</div>
</div>
{% block extrascripts %}
<script type="text/javascript">
$(function () {
$(".upload-file").modalForm({formURL: "{% url 'file-upload' %}"});
});
</script>
{% endblock extrascripts %}
urls.py
path('upload/file/', FileCreateView.as_view(), name="file-upload"),
model.py
class File(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
author = models.ForeignKey(User, on_delete=models.CASCADE)
visible_to_home = models.ManyToManyField(Home, blank=True) # when none visible to all home
visible_to_company = models.ManyToManyField(Company, blank=True) # when none visible to all company
# To determine visibility, check if vtc is none or include company of user and if true, check same for home
created_date = models.DateTimeField(auto_now=True)
published = models.BooleanField(default=True)
upload = models.FileField(blank=True, null=True, upload_to=update_filename)
title = models.CharField(max_length=225, blank=True, null=True)
description = models.TextField(blank=True, null=True)
In one of the uploaded file there is no upload
field of the model and the next of the twice there is everything.
django django-forms django-views
I wanted to upload any file. But when I submit the upload then same post is uploaded twice but in one of it there is no file but just the content and in next there is file.
class FileCreateView(PassRequestMixin, SuccessMessageMixin,
CreateView):
template_name = 'file/upload-file.html'
form_class = FileForm
success_message = 'File was uploaded successfully'
success_url = reverse_lazy('home')
def post(self, *args, **kwargs):
"""
Handle POST requests: instantiate a form instance with the passed
POST variables and then check if it's valid.
"""
# form = self.get_form([self.request.FILES])
form = self.form_class(self.request.POST, self.request.FILES)
if self.request.method == 'POST':
if form.is_valid():
file = form.save(commit=False)
file.upload = form.cleaned_data['upload']
file.author = User.objects.get(pk=self.request.user.pk)
file.save()
return self.form_valid(form)
else:
return self.form_invalid(form)
home.html
<div class="container mt-3">
<div class="row">
<div class="col-12 mb-3">
<button class="upload-file btn btn-primary" type="button" name="button">
<span class="fa fa-plus mr-2"></span>Upload Form</button>
</div>
</div>
{% block extrascripts %}
<script type="text/javascript">
$(function () {
$(".upload-file").modalForm({formURL: "{% url 'file-upload' %}"});
});
</script>
{% endblock extrascripts %}
urls.py
path('upload/file/', FileCreateView.as_view(), name="file-upload"),
model.py
class File(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
author = models.ForeignKey(User, on_delete=models.CASCADE)
visible_to_home = models.ManyToManyField(Home, blank=True) # when none visible to all home
visible_to_company = models.ManyToManyField(Company, blank=True) # when none visible to all company
# To determine visibility, check if vtc is none or include company of user and if true, check same for home
created_date = models.DateTimeField(auto_now=True)
published = models.BooleanField(default=True)
upload = models.FileField(blank=True, null=True, upload_to=update_filename)
title = models.CharField(max_length=225, blank=True, null=True)
description = models.TextField(blank=True, null=True)
In one of the uploaded file there is no upload
field of the model and the next of the twice there is everything.
django django-forms django-views
django django-forms django-views
asked Jan 1 at 7:39
Bishwa KarkiBishwa Karki
70112
70112
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993825%2ffrom-submitted-twice%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993825%2ffrom-submitted-twice%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown