Can anyone help me with this issue? I saw all posts which are similar to my issue, but I can't fix this up.
Error:
Reverse for 'commit_add' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['git/project/(?P<pk>[0-9]+)/add_commit/$']
views.py
class CommitCreate(CreateView):
template_name = 'layout/project_commit_detail.html'
model = Commit
fields = ['user', 'project', 'branch', 'commit_title', 'commit_body']
success_url = reverse_lazy('git_project:index')
html form
<div class="container-fluid">
Add New Commit
<div id="demo1" class="collapse" >
<form class="form-horizontal" action="{% url 'git_project:commit_add' project.id %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% if user.is_authenticated %}
<label for="user">Commit created by: "{{ user.username }}"</label><br>
<input id="user" type="hidden" name="user" value="{{ user.id }}">
<label for="project">Commit for project: "{{ project.proj_title }}"</label><br>
<input id="project" type="hidden" name="project" value="{{ project.id }}">
<label for="branch">Branch: </label>
<select>
{% for branch in all_branches %}
<option id="branch" name="branch">{{branch.branch_name}}</option>
{% endfor %}
</select><br>
<label for="commit_title">Commit Title: </label>
<input id="commit_title" type="text" name="commit_title"><br>
<textarea id="commit_body" name="commit_body" rows="5" cols="50" placeholder="Commit body..."></textarea><br>
<button type="submit" class="btn btn-success">Commit</button>
{% endif %}
</form>
</div>
url.py
url(r'project/(?P<pk>[0-9]+)/add_commit/$', views.CommitCreate.as_view(), name='commit_add'),
model.py
class Commit(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, default=0)
project = models.ForeignKey(Project, on_delete=models.CASCADE, default=0)
branch = models.ForeignKey(Branch, on_delete=models.CASCADE, default=0)
commit_title = models.CharField(max_length=64)
commit_body = models.CharField(max_length=16384)
commit_date = models.DateTimeField(default=timezone.now)
I don't know why is happening. Can anyone help me? I am very new in Django.
Thanks! :)
Related
I am displaying a form for a posting which has already been saved to the database. I am giving the user the option to view the values as they are in the database and give him the option to edit them. However, it is not working for dropdown. I have tried writing something but it does not display the correct values.
please help :(((
Here is the code snippet:
<div class="form-group col-md-6">
<label for="industry_type">Industry Type</label>
<select class="form-control" id="industry_type" name="industry_type" selected="
{{ internship.industry_type }}">
{% for ind_type in industry_types %}
<option value="{{ ind_type }}" {% if '{{ ind_type }}' == '{{
internship.industry_type }}' %}selected="selected" {% endif %}>
{{ind_type}}</option>
{% endfor %}
</select>
</div>
views.py
def edit_internship(request, pid):
internship = Internship.objects.get(id=pid)
skills = InternshipSkill.objects.filter(internship=internship)
emp_steps = InternshipEmpStep.objects.filter(internship=internship)
.order_by('emp_step_no')
industry_types = IndustryType.objects.all()
context = {
'industry_types': industry_types,
'internship': internship,
'skills': skills,
'emp_steps': emp_steps,
}
return render(request, 'edit_internship.html', context)
Okay this is just because don't need to re-add the curly brackets in django's templates if you already have them:
<div class="form-group col-md-6">
<label for="industry_type">Industry Type</label>
<select class="form-control" id="industry_type" name="industry_type" selected="{{ internship.industry_type.pk }}">
{% for ind_type in industry_types %}
<option value="{{ ind_type.pk }}" {% if ind_type.pk == internship.industry_type.pk %}selected="selected" {% endif %}>
{{ ind_type }}
</option>
{% endfor %}
</select>
</div>
Also, don't forget the .pk argument to ind_type when declaring the value.
For future reference, I really recommend you use Django's forms when creating these, it makes your like SO much easier:
views.py
class EditInternship(UpdateView):
model = Internship
template_name = 'edit_internship.html'
form_class = EditInternshipForm
def get_context_data(**kwargs):
ctx = super().get_context_data(**kwargs)
ctx.update(
skills=InternshipSkill.objects.filter(internship=internship),
emp_steps=InternshipEmpStep.objects.filter(internship=internship).order_by('emp_step_no')
)
# You'll also have `form` already in the context
return ctx
edit_internship = EditInternship.as_view()
forms.py
class EditInternshipForm(forms.ModelForm):
class Meta:
model = Internship
fields = 'industry_type', 'other_fields_on_the_model_you_want_to_edit'
edit_internship.html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.industry_type }}
{{ form.other_fields_you_want_to_edit }}
<input type="submit" name="submit_button" value="Save" class="btn btn-primary btn-sm" id="submit_button">
</form>
I had the same doubt of radio button in model file.
I want it to be a compulsory/required field with default selection to "No".
Fields available would be - Yes/No. Any suggestions.
B_CHOICES = [('P','Positive'),('N','Negative')]
Tag_Type = models.CharField(choices=B_CHOICES, max_length=128)
Here's my form and model code-
Form -
<form id="ant_form" name="ant_form" method="POST" action="#">{% csrf_token %}
<input name="r_user" type="checkbox" value="{{user.id}}" /> {{user.username}}
<input type="radio" name="txt{{user.id}}" value="P" > </input>
P Comment
<input type="radio" name="txt{{user.id}}" value="N" > </input>
N Comment
<textarea style="height: auto" rows="1" id="txt{{user.id}}" name="comment_{{user.id}}" cols="50">
</textarea>
<br>
</li>
{% endfor %}
</ul>
</div>
{% endif %} {# users #}
</form>
Model -
class r(models.Model):
BCHOICES = [('P','P'),('N','N'),]
tag_type = models.CharField(max_length=2, default=Negative, choices=BCHOICES)
You can set the default to 'N':
from django.db import models
B_CHOICES = [('P','Positive'),('N','Negative')]
class MyModel(models.Model):
tag_type = models.CharField(choices=B_CHOICES, max_length=128, default='N')
When you render a ModelForm for the given MyModel, then the HTML will look like:
<select name="tag_type" id="id_tag_type">
<option value="P">Positive</option>
<option value="N" selected>Negative</option>
</select>
I know my code is not optimal as I'm only a beginner with Django so don't be too hard on me
Here is my code :
views.py
class ComposantUpdate(UpdateView):
model = configuration
fields = '__all__'
template_name = 'accueil/exploitation/update_composant.html'
update_composant.html
<form action="{% url 'composant_update' pk=composant.id %}" method="post">
{% csrf_token %}
<div class="form-group">
{% for field in form %}
<label class="col-md-6 offset-md-3 nom_champ"> {{field.label_tag}}</label>
<input class="col-md-4 offset-md-4 contenu_champ" type="text" name="{{ field.label }}" id="{{ field.id_for_label }}" value="{{ field.value }}"/>
{%endfor%}
<br>
<br>
<button class="col-md-6 offset-md-3 btn btn-primary" type="submit" value="Update" /> Mettre à jour </button>
</div>
</form>
urls.py
path('update_composant/<int:pk>', views.ComposantUpdate.as_view(),
name='composant_update'),
models.py
class configuration(models.Model):
Num_ordre = models.CharField(max_length=15)
Composant = models.CharField(max_length=15)
Designation = models.CharField(max_length=15)
Qte_servie = models.IntegerField()
Qte_a_servir = models.IntegerField()
Lot = models.CharField(max_length=15)
Categorie = models.CharField(max_length=15)
Famille = models.CharField(max_length=15)
def __str__(self):
return '%s %s %s' % (self.Num_ordre, self.Designation, self.Lot)
when I go to the url of the form, all the fields get the content of the database which is what I expect but if I modify the "Lot" field and click to submit, my database is not updated at all with the new value of "Lot". DO you have any idea of why ? For information, I do get redirected to the 'composant_update' view and no error is risen
In fact, I didn't write my html correctly, the attribute "name" in the input should have been {{field.html_name}} instead of {{field.label}}.
This doesn't work :
<input class="col-md-4 offset-md-4 contenu_champ" type="text" name="{{ field.label }}" id="{{ field.id_for_label }}" value="{{ field.value }}"/>
This works :
<input class="col-md-4 offset-md-4 contenu_champ" type="text" name="{{ field.html_name}}" id="{{ field.id_for_label }}" value="{{ field.value }}"/>
See https://docs.djangoproject.com/en/2.0/topics/forms/#s-looping-over-the-form-s-fields for more information
Thanks Willem Van Onsem !
Can you help me with this issue? I want to pass data from HTML form to Database.
This is my HTML form:
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% if user.is_authenticated %}
<label for="username">Username: </label>
<input id="username" type="text" name="username" value="{{ user.username }}" disabled><br>
<label for="image">Image: </label>
<input id="image" type="file" name="image">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Save Changes</button>
</div>
</div>
{% endif %}
</form>
And this is my Views.py
class CreateProfile(CreateView):
template_name = 'layout/add_photo.html'
model = Profile
fields = ['image', 'user']
success_url = reverse_lazy('git_project:index')
When I fill in the form (username is equal to logged in username, and chose an image) and when I click "Save Changes", the data has to be passed in Database in table Profile. Here is class Profile in models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.FileField()
I know this is basic, but I don't know how to fix it up, so please, help! :)
I'm trying to submit a form with just the date(and now time), but when I go to submit it I get the error incidents_incident.incident_date_time_occurred may not be NULL even when I enter an actual date in the input field. Why do I get that error when I enter in a date?
models.py
class Incident(models.Model):
incident_date_time_occurred = models.DateTimeField('incident occurred', default=timezone.now, blank=True)
class Meta:
verbose_name_plural="Incident"
forms.py
class IncidentForm(forms.ModelForm):
class Meta:
model = Incident
fields = ('incident_date_time_occurred',)
report.html
{% extends "base.html" %}
{% block content %}
<div class="container-fluid">
<form action="{% url 'incidents:report' %}" method="POST">{% csrf_token %}
{% csrf_token %}
<div class="row">
<div class="form-group col-md-4">
<label for="date" class="col-sm-4 control-label">{{ form.incident_date_time_occurred.label }}</label>
<input type="datetime-local" name= "incident_date_time_occurred" class="form-control" id="date">
</div>
</div>
<input type="submit" value="Inschrijven!" class="btn btn-primary" />
</form>
</div>
{% endblock %}
Can you check your solution by using default form created from incident? As I remember Django's dateTimeField requires two fields: date and time.
https://docs.djangoproject.com/en/1.7/ref/models/fields/#datetimefield
You are missing the name attribute on the date input. Try that:
<input type="date" class="form-control" id="date" name="incident_date_time_occurred">