models.py
from django.db import models
from django.urls import reverse
# Create your models here.
class WellInfo(models.Model):
api = models.CharField(max_length=100, primary_key=True)
well_name = models.CharField(max_length=100)
status = models.CharField(max_length=100)
phase = models.CharField(max_length=100)
region = models.CharField(max_length=100)
start_date = models.CharField(max_length=100)
last_updates = models.CharField(max_length=100)
def __str__(self):
return self.well_name
views.py
class ContextualMainView(TemplateView):
template_name = 'contextual_main.html'
class WellList_ListView(ListView):
template_name = 'well_list.html'
context_object_name = 'well_info'
model = models.WellInfo
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# get string representation of field names in list
context['fields'] = [field.name for field in models.WellInfo._meta.get_fields()]
# nested list that has all objects' all attribute values
context['well_info'] = [[getattr(instance, field) for field in context['fields']] for instance in context['well_info']]
return context
html
<thead>
<tr>
{% for field in fields %}
<th>{{ field }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for well in well_info %}
<tr>
{% for value in well %}
<td><a href="{% url 'eric_base:contextual' api=well.api %}">{{ value }}</a></td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
app / urls.py
from django.urls import re_path, include
from django.contrib.auth import views as auth_views
from eric_base import views as base_views
app_name = 'eric_base'
urlpatterns = [
re_path(r'^(?P<api>\d+)/$', base_views.ContextualMainView.as_view(), name='contextual'),
]
在我的models.py
中,我将api
设置为主键,并希望使用api
作为唯一的URL标识符,并将其附加在url的末尾,如下所示:
http://127.0.0.1:8000/well_list/contextual/api_number_1
http://127.0.0.1:8000/well_list/contextual/api_number_2
http://127.0.0.1:8000/well_list/contextual/api_number_3
我认为我的views.py
或models.py
中缺少某些内容,但我不知道它是什么。我该如何解决?
答案 0 :(得分:0)
您的正则表达式function drawPath(source, desti, flag) {
/*
* Define context
*/
//lower
if(!flag){
var c = document.getElementById("myCanvas");
context = c.getContext("2d");
//upper
} else {
var cUpr = document.getElementById("myCanvasUpr");
context = cUpr.getContext("2d");
}
/*
* Clear the variables
*/
points = [];
secondary_points = [];
vertices = [];
secondary_vertices = [];
t = 1;
done = false;
//check for invalid locations
if (source != "" && desti != "") {
context.lineCap = 'round';
context.beginPath();
/*
* Get the coordinates from source and destination strings
*/
var src = dict[source];
var dst = dict[desti];
/*
* Get the point number of the point on the path that the source and destination connect to
*/
var begin = point_num[source];
var finish = point_num[desti];
/*
* Draw the green and red starting/ending circles (green is start, red is end)
*/
context.beginPath();
context.arc(src[0], src[1], 8, 0, 2 * Math.PI);
context.fillStyle = 'green';
context.fill();
context.beginPath();
context.arc(dst[0], dst[1], 6, 0, 2 * Math.PI);
context.fillStyle = 'red';
context.fill();
/*
* Call the function that draws the entire path
*/
draw_segments(begin, finish, src, dst, flag);
//window.alert(JSON.stringify(vertices, null, 4))
/*
* Edit what the line looks like
*/
context.lineWidth = 5;
context.strokeStyle = "#ff0000";
context.stroke();
}
}
是一个数字,但是(?P<api>\d+)
字段是一个api
。
尝试CharField
,它将匹配所有内容,而不仅仅是数字。